Twitter id_str未定义方法Ruby中的#map错误

时间:2013-12-09 01:26:53

标签: ruby twitter map enumeration

我正在尝试映射一个来自Twitter API的枚举器。我可以使用test.map(&:attrs)成功映射整个枚举,并且我可以映射特定字段但是当我尝试映射id_str字段时,我得到一个未定义的方法错误。我无法弄清楚我的语法是否关闭或是否有其他注意事项。

以下是我的示例代码和输出:

print "original output\n"
print test.map(&:attrs)

print "\n\nmap a few fields\n"
print test.map { |e| {  id: e.id, 
                        name: e.name, 
                        screen_name: e.screen_name
                      }}
print "\n\nid_str seems to return a undefined method\n"                    
print test.map { |e| {  id: e.id, 
                        id_str: e.id_str,
                        name: e.name, 
                        screen_name: e.screen_name
                      }}

生成以下输出:

original output
[{:id=>78194111, :id_str=>"78194111", :name=>"Chelsea Peretti", :screen_name=>"ChelseaVPeretti", :location=>"Los Angeles", :description=>"One of the greats!", :url=>"http://t.co/3rRz8qGpeW", :entities=>{:url=>{:urls=>[{:url=>"http://t.co/3rRz8qGpeW", :expanded_url=>"http://www.chelseaperetti.com", :
display_url=>"chelseaperetti.com", :indices=>[0, 22]}]}, :description=>{:urls=>[]}}, :protected=>false, :followers_count=>249943, :friends_count=>740, :listed_count=>4277, :created_at=>"Tue Sep 29 02:35:35 +0000 2009", :favourites_count=>33016, :utc_offset=>-28800, :time_zone=>"Pacific Time (US & Ca
nada)", :geo_enabled=>true, :verified=>true, :statuses_count=>14958, :lang=>"en", :status=>{:created_at=>"Mon Dec 09 00:40:48 +0000 2013", :id=>409845047744409600, :id_str=>"409845047744409600", :text=>"Really looking forward to spending half my life in an apple store", :source=>"<a href=\"http://tw
itter.com/download/iphone\" rel=\"nofollow\">Twitter for iPhone</a>", :truncated=>false, :in_reply_to_status_id=>nil, :in_reply_to_status_id_str=>nil, :in_reply_to_user_id=>nil, :in_reply_to_user_id_str=>nil, :in_reply_to_screen_name=>nil, :geo=>nil, :coordinates=>nil, :place=>nil, :contributors=>ni
l, :retweet_count=>13, :favorite_count=>77, :entities=>{:hashtags=>[], :symbols=>[], :urls=>[], :user_mentions=>[]}, :favorited=>false, :retweeted=>false, :lang=>"en"}, :contributors_enabled=>false, :is_translator=>false, :profile_background_color=>"022330", :profile_background_image_url=>"http://a0
.twimg.com/profile_background_images/777102099/405580aee5a6a6d3f4d608b5bc488149.jpeg", :profile_background_image_url_https=>"https://si0.twimg.com/profile_background_images/777102099/405580aee5a6a6d3f4d608b5bc488149.jpeg", :profile_background_tile=>true, :profile_image_url=>"http://pbs.twimg.com/pro
file_images/378800000812827132/fe8566998e61c0f3e1275af4953a22e9_normal.jpeg", :profile_image_url_https=>"https://pbs.twimg.com/profile_images/378800000812827132/fe8566998e61c0f3e1275af4953a22e9_normal.jpeg", :profile_banner_url=>"https://pbs.twimg.com/profile_banners/78194111/1382904580", :profile_l
ink_color=>"0084B4", :profile_sidebar_border_color=>"FFFFFF", :profile_sidebar_fill_color=>"C0DFEC", :profile_text_color=>"333333", :profile_use_background_image=>true, :default_profile=>false, :default_profile_image=>false, :following=>false, :follow_request_sent=>false, :notifications=>false}]

map a few fields
[{:id=>78194111, :name=>"Chelsea Peretti", :screen_name=>"ChelseaVPeretti"}]

id_str seems to return a undefined method
C:/RailsInstaller/AppCode/first attempt at graph.rb:142:in `block in <main>': undefined method `id_str' for #<Twitter::User:0x2b602f0> (NoMethodError)
        from C:/RailsInstaller/AppCode/first attempt at graph.rb:141:in `map'
        from C:/RailsInstaller/AppCode/first attempt at graph.rb:141:in `<main>'

1 个答案:

答案 0 :(得分:1)

我自己在Ruby中使用Twitter API很新,但事实证明id_str不是你想要枚举的Ruby Twitter::User对象的方法。快速检查特定的一个,例如,test.first.methods将确认这一点。

这意味着您无法使用点表示法访问id_str。但它是test.map(&:attrs)返回的哈希的关键,因此您可以通过以下方式修改上述代码:

print test.map { |e| { id: e.attrs[:id],
                       id_str: e.attrs[:id_str],
                       name: e.attrs[:name],
                       screen_name: e.attrs[:screen_name]
               }}

如果你没有使用点表示法,你可以轻松地将每个哈希对象转换为OpenStruct,如下所示:

friends = test.map { |f| OpenStruct.new(f.attrs) }

然后你就可以做friends.map(&:id_str)

我认为上面的示例代码存在混淆。在第一行中,输出是一个哈希数组。但是你要映射的是一组Twitter::User个对象。前者以id_str为关键,但后者没有这样的方法。