访问哈希元素时的未定义方法

时间:2013-08-19 14:31:55

标签: ruby-on-rails ruby linkedin

哈希:

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}

尝试调用p.pid但收到错误:

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

可以正常访问所有其他元素。也为该领域尝试了不同的名称但无济于事。有人可以对此有所了解吗?真的希望它不是你长时间盯着的那些错误之一,只是意识到它是愚蠢的:/。

注意:我也试过p ['pid']。这也不起作用。 Rails相对较新。

1 个答案:

答案 0 :(得分:21)

尝试这样的事情:

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