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?
I don't know if that's what you want, but maybe you could do this?
Do:
Also at each step, you can use an appropriate method built in
NilClass
to escape fromnil
, if it were array, string, or numeric. Just addto_hash
to the inventory of this list and use it.Write the ugliness once, then hide it
Are you able to avoid using a multi-dimensional hash, and use
or
instead?