0

我正在构建一个与 Highrise 集成的应用程序,并且到目前为止非常好,但是,当尝试导入一个人时Highrise::People,它会吐出一个类似于这样的巨大哈希:

[ ...,  #<Highrise::Person:0x1035084b8 @attributes={"contact_data"=>#<Highrise::Person::ContactData:0x1034f8b30 @attributes={"email_addresses"=>[], "addresses"=>[], "web_addresses"=>[], "phone_numbers"=>[], "twitter_accounts"=>[], "instant_messengers"=>[]}, @prefix_options={}>, "created_at"=>Sat Nov 28 05:38:26 UTC 2009, "title"=>"president", "updated_at"=>Sat Nov 28 05:38:27 UTC 2009, "background"=>"asdfasdfadsfas", "id"=>27569370, "owner_id"=>nil, "group_id"=>nil, "company_id"=>27569371, "last_name"=>"Doe", "author_id"=>192208, "visible_to"=>"Everyone", "first_name"=>"John"}, @prefix_options={}>, ... ]

看起来每个内部Highrise::Person都是一个@attributeswhich is another Highrise::Person::ContactData,它本身就是另一个@attributes带有数组的email_addresses[]phone_numbers[]以及简单的键/值......

很抱歉让您感到困惑,我想知道的是如何first_name从这样的哈希中获取每个人的

可能超级简单我只是感到困惑......

更新

我想一个更好的表达方式是,鉴于上面的哈希,为什么这不起作用:

@people = Highrise::Person.find(:all)

for person in @people do
   person.attributes["first_name"]
end
4

2 回答 2

1

如果你的列表是一个Person数组

yourlist.each do |person|
    puts person.attributes["first_name"]
end

如果Person有一个 attr_reader,那就是。

于 2009-11-28T19:53:12.387 回答
0

你也可以这样做:

@people.each do |person|
    puts person.first_name
end
于 2012-08-16T16:06:32.297 回答