i need to pull out some specific values from a nested hash, but nils are driving me crazy. in the following example, if address_details, country, administrative_area, locality OR postal_code are nil i get "The error occurred while evaluating nil.[]" zip = placemark[''address_details''][''country''][''administrative_area''] [''locality''][''postal_code''][''postal_code_number''] i tried unless placemark[''address_details''][''country''][''administrative_area''] [''locality''][''postal_code''][''postal_code_number''].nil? but if anything above postal_code_number is nil i still get the error. i could check each level but that is really ugly. is there a easy way to check if postal_code_number exists?
What value should ''zip'' contain if postal_code_number doesn''t exist (or any of the keys above it)? If one of the keys in that long nasty chain are nil, you''re going to get an exception, but if you know an acceptable default value in case you can''t eval the whole expression, rescue the exception and return what you want. If you want it to be an empty string, for instance, you could do: zip placemark[''address_details''][''country''][''administrative_area''][''locality''][''postal_code''][''postal_code_number''] rescue '''' On Tue, May 12, 2009 at 12:53 AM, scott <scottnj-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > i need to pull out some specific values from a nested hash, but nils > are driving me crazy. in the following example, if address_details, > country, administrative_area, locality OR postal_code are nil i get > "The error occurred while evaluating nil.[]" > > zip = placemark[''address_details''][''country''][''administrative_area''] > [''locality''][''postal_code''][''postal_code_number''] > > i tried > > unless placemark[''address_details''][''country''][''administrative_area''] > [''locality''][''postal_code''][''postal_code_number''].nil? > > but if anything above postal_code_number is nil i still get the error. > i could check each level but that is really ugly. > > is there a easy way to check if postal_code_number exists? > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
that will work perfect, thanks On May 12, 3:01 am, Ryan Waldron <r...-L2nCScK6t0HQT0dZR+AlfA@public.gmane.org> wrote:> What value should ''zip'' contain if postal_code_number doesn''t exist (or any > of the keys above it)? > > If one of the keys in that long nasty chain are nil, you''re going to get an > exception, but if you know an acceptable default value in case you can''t > eval the whole expression, rescue the exception and return what you want. > > If you want it to be an empty string, for instance, you could do: > > zip > placemark[''address_details''][''country''][''administrative_area''][''locality''][ ''postal_code''][''postal_code_number''] > rescue '''' > > > > On Tue, May 12, 2009 at 12:53 AM, scott <scot...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > i need to pull out some specific values from a nested hash, but nils > > are driving me crazy. in the following example, if address_details, > > country, administrative_area, locality OR postal_code are nil i get > > "The error occurred while evaluating nil.[]" > > > zip = placemark[''address_details''][''country''][''administrative_area''] > > [''locality''][''postal_code''][''postal_code_number''] > > > i tried > > > unless placemark[''address_details''][''country''][''administrative_area''] > > [''locality''][''postal_code''][''postal_code_number''].nil? > > > but if anything above postal_code_number is nil i still get the error. > > i could check each level but that is really ugly. > > > is there a easy way to check if postal_code_number exists?