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

标签 ruby twitter dictionary 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>'

最佳答案

我自己对使用 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作为key,后者没有这样的方法。

关于ruby - Ruby 中的 Twitter id_str 未定义方法 #map 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20461575/

相关文章:

Ruby Mechanize gem,从本地 html 副本恢复 Mechanize::Page 对象

c++元素在存储在 map 中时丢失

ruby - 检查 HashMap 中的键是否都在数组中

ruby-on-rails - 如何在 x 位数后添加空格?

ruby-on-rails - ruby net-sftp 逐行读取文件

ios - 以编程方式在 Twitter 中获取 "Allow these Apps to use your account"的值

iphone/ipad 标准 twitter oauth 模块?

android - 使用两个 Linkify 和可点击的网络链接的一个 TextView 的文本颜色

c++ - 迭代 map

python 字典更新差异