问题
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.
回答1:
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
来源:https://stackoverflow.com/questions/18316384/undefined-method-when-accessing-hash-element