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



我正在尝试映射来自Twitter API的枚举器。我可以使用test.map(&:attrs)成功地映射整个枚举表,并且我可以映射特定的字段,但是当我尝试映射id_str字段时,我会遇到未定义的方法错误。我不知道我的语法是否错误,或者是否有其他考虑因素。

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

print "original outputn"
print test.map(&:attrs)
print "nnmap a few fieldsn"
print test.map { |e| {  id: e.id, 
                        name: e.name, 
                        screen_name: e.screen_name
                      }}
print "nnid_str seems to return a undefined methodn"                    
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>'

在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作为密钥,但后者没有这样的方法。

最新更新