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?
Ruby 2.3.0 introduced a new method called
dig
on bothHash
andArray
that solves this problem entirely.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:
The best compromise between functionality and clarity IMO is Raganwald's
andand
. With that, you would do: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.Dangerous but works:
We can new do
The "h_try" chain follows similar style to a "try" chain.
If you wanna get into monkeypatching you could do something like this
Then a call to
params[:company][:owner][:name]
will yield nil if at any point one of the nested hashes is nil.EDIT: If you want a safer route that also provides clean code you could do something like
The code would look like this:
params.chain(:company, :owner, :name)
I would write this as:
It's not as clean as the ? operator in Io, but Ruby doesn't have that. The answer by @ThiagoSilveira is also good, though it will be slower.