0

我正在使用通过api 版本 2查询的rMeetup gem ,但我不明白如何从响应中提取值。这是我卡住的地方(用于此示例):"members"irb

>> require 'rmeetup'
=> true
>> client = RMeetup::Client.new do |config| config.api_key = "LALAMYKEYNOTYOURS" end
=> #<RMeetup::Client:0x007fbda4b58060 @configuration=#<RMeetup::Configuration:0x007fbda4b63fa0 @api_key="LALAMYKEYNOTYOURS">>
>> results = client.fetch(:groups, {:group_urlname => 'San-Francisco-Riak-Meetup'})
=> [#<RMeetup::Type::Group:0x007fbda4b80088 @group={"utc_offset"=>-25200000, "country"=>"US", "visibility"=>"public", "city"=>"San Francisco", "timezone"=>"US/Pacific", "created"=>1278976613000, "topics"=>[{"urlkey"=>"opensource", "name"=>"Open Source", "id"=>563}, {"urlkey"=>"web", "name"=>"Web Technology", "id"=>10209}, {"urlkey"=>"big-data", "name"=>"Big Data", "id"=>18062}, {"urlkey"=>"database-development", "name"=>"Database Development", "id"=>21506}, {"urlkey"=>"erlang-programming", "name"=>"Erlang Programming", "id"=>46514}, {"urlkey"=>"nosql", "name"=>"NoSQL", "id"=>58162}, {"urlkey"=>"riak", "name"=>"Riak", "id"=>112355}, {"urlkey"=>"distributed-systems", "name"=>"Distributed Systems", "id"=>113032}], "link"=>"http://www.meetup.com/San-Francisco-Riak-Meetup/", "rating"=>4.57, "description"=>"<p>A monthly meetup for those in the Bay Area to talk Riak, distributed systems, and app. development.</p>", "lon"=>-122.4000015258789, "group_photo"=>{"highres_link"=>"http://photos4.meetupstatic.com/photos/event/e/6/9/e/highres_16559038.jpeg", "photo_id"=>16559038, "photo_link"=>"http://photos4.meetupstatic.com/photos/event/e/6/9/e/600_16559038.jpeg", "thumb_link"=>"http://photos2.meetupstatic.com/photos/event/e/6/9/e/thumb_16559038.jpeg"}, "join_mode"=>"open", "organizer"=>{"member_id"=>140545442, "name"=>"Basho"}, "members"=>696, "name"=>"San Francisco Riak Meetup", "id"=>1674527, "state"=>"CA", "urlname"=>"San-Francisco-Riak-Meetup", "category"=>{"name"=>"tech", "id"=>34, "shortname"=>"tech"}, "lat"=>37.790000915527344, "who"=>"Riaktors"}>]
>> results.each do |k| puts k["members"] end

这可能是我对如何@group在这个结果中查询的误解。尽管在 SO 和其他网站上有类似的问题,我还没有找到任何可以澄清它的东西。

4

1 回答 1

0

我今天想通了。这是将方法与哈希混淆的示例。正确的语法是:

results.each do |k| puts k.members end

因为members是 Meetup::Type::Group 的一个方法,至少从它的外观上看是这样。这不是它的记录方式,但它是有效的。

如果是哈希,原始语法k["members"]将起作用:k

irb(main):027:0> k = {"members" => 1000}
=> {"members"=>1000}
irb(main):028:0> k["members"]
=> 1000
于 2015-12-15T16:28:31.263 回答