Try works pretty well for this. hash.try(:[], :data).try(:[], :details).try(:[], :id)
Or hash[:data][:details][:id] rescue nil On Jul 27, 2014 4:54 PM, "Rodrigo Rosenfeld Rosas" <[email protected]> wrote: > Take a look at the Configatron gem. > Em 27/07/2014 17:02, "James Coleman" <[email protected]> escreveu: > >> The andand gem will allow you to write code like: >> >> hash.andand[:key].andand[:key2] >> >> where anything on a nil object returns a proxy that accepts any method >> and returns nil. This is far safer than what you propose as it won't affect >> anyone. >> >> In general though, while this may seem convenient, it makes reasoning >> about code significantly more difficult. The more code you write, the more >> I believe that you find that we need *more* explicit handling of nil not >> less. >> >> >> On Sun, Jul 27, 2014 at 1:52 PM, Sergio Campamá <[email protected]> >> wrote: >> >>> I think a better approach is >>> >>> return hash && hash[:data] && hash[:data][:details] && >>> hash[:data][:details][:id] >>> >>> That will return the value or nil if the chain was broken at any >>> point. I know it's not the same, but much less code than the example. >>> -------------------------------------- >>> Sergio Campamá >>> [email protected] >>> >>> >>> >>> On Sun, Jul 27, 2014 at 10:48 AM, Steve Klabnik <[email protected]> >>> wrote: >>> > This has a very large potential to break a very, very large amount of >>> code. >>> > >>> > -- >>> > You received this message because you are subscribed to the Google >>> Groups "Ruby on Rails: Core" group. >>> > To unsubscribe from this group and stop receiving emails from it, send >>> an email to [email protected]. >>> > To post to this group, send email to [email protected] >>> . >>> > Visit this group at http://groups.google.com/group/rubyonrails-core. >>> > For more options, visit https://groups.google.com/d/optout. >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Ruby on Rails: Core" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to [email protected]. >>> To post to this group, send email to [email protected]. >>> Visit this group at http://groups.google.com/group/rubyonrails-core. >>> For more options, visit https://groups.google.com/d/optout. >>> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Ruby on Rails: Core" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> To post to this group, send email to [email protected]. >> Visit this group at http://groups.google.com/group/rubyonrails-core. >> For more options, visit https://groups.google.com/d/optout. >> > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Core" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/rubyonrails-core. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/d/optout.
