Undefined method when accessing hash element

Kirsty Williams picture Kirsty Williams · Aug 19, 2013 · Viewed 20.6k times · Source

Hash:

p: {:headline=>"Managing Director at Test company name", :pid=>"0tSsRvCR7r", :first_name=>"John", :last_name=>"Doe", :industry=>"Financial Services", :summary=>nil, :public_profile_url=>"http://www.linkedin.com/pub/john-doe/7a/78/606", :distance=>0}

Attempting to call p.pid but getting the error:

EXCEPTION: undefined method `pid' for #<Hash:0x007fcf1b3a29f0> 

All other elements can be accessed fine. Also tried different names for the field but to no avail. Can anyone shed some light on this please? Really hoping it's not one of those bugs that you stare at for ages only to realise it's something silly :/.

Note: I have also tried p['pid']. This didn't work either. Relatively new to Rails.

Answer

ajt picture ajt · Aug 19, 2013

Try something like this :

p = {:headline=>"Managing Director at Test company name", :pid=>"0tSsRvCR7r", :first_name=>"John",     :last_name=>"Doe", :industry=>"Financial Services", :summary=>nil,     :public_profile_url=>"http://www.linkedin.com/pub/john-doe/7a/78/606", :distance=>0}
puts p
puts p[:pid]

hash docs

more on hashes