How to avoid NoMethodError for missing elements in nested hashes, without repeated nil checks?
I'm looking for a good way to avoid checking for nil
at each level in deeply nested hashes. For example:
name = params[:company][:owner][:name] if params[:company] && params[:company][:owner] && params[:company][:owner][:name]
This requires three checks, and makes for very ugly code. Any way to get around this?
Solution 1:
Ruby 2.3.0 introduced a new method called dig
on both Hash
and Array
that solves this problem entirely.
name = params.dig(:company, :owner, :name)
It returns nil
if the key is missing at any level.
If you are using a version of Ruby older than 2.3, you can use the ruby_dig gem or implement it yourself:
module RubyDig
def dig(key, *rest)
if value = (self[key] rescue nil)
if rest.empty?
value
elsif value.respond_to?(:dig)
value.dig(*rest)
end
end
end
end
if RUBY_VERSION < '2.3'
Array.send(:include, RubyDig)
Hash.send(:include, RubyDig)
end
Solution 2:
The best compromise between functionality and clarity IMO is Raganwald's andand
. With that, you would do:
params[:company].andand[:owner].andand[:name]
It's similar to try
, but reads a lot better in this case since you're still sending messages like normal, but with a delimiter between that calls attention to the fact that you're treating nils specially.