Undefined method when accessing hash element

谁说我不能喝 提交于 2020-06-09 16:59:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!