Hi guys,

trying to do something like obj[:id] when obj is nil, returns an error.

Things are even more complicated when you try to work with nested hashes. 
To access something like hash[:data][:details][:id] you need to write

   if hash.has_key?(:data) && hash[:data].has_key?(:details)
      return hash[:data][:details][:id]
   end

In my code I monkey-patched the NilClass so that the nested accesses always 
return nil if they do not exists instead of throwing an error. It 
simplified a lot my code, which is now way cleaner. The patch was something 
like

   class NilClass
     def [](*keys)
       nil
     end
   end

I thought I would bring this up to your attention. It may be worth 
introducing it in Rails as default.

-- 
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.

Reply via email to