我正在构建一个与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 里面是 @attributes ,这是另一个 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
有帮助吗?

解决方案

如果 yourlist 是人员数组

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

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

其他提示

你也可以这样做:

@people.each do |person|
    puts person.first_name
end
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top