2

我正在使用Twitter Gem访问 Twitter API,并且我想创建一个变量,该变量仅mentions根据提及的文本存储唯一的变量。

现在,我像这样存储所有提及:@allmentions = Twitter.mentions_timeline

这是返回的提及示例@allmentions[0]

 => #<Twitter::Tweet:0x007fbffb59ab88 @attrs={:created_at=>"Mon Dec 10 01:28:11 +0000 2012", :id=>277947788216639488, :id_str=>"277947788216639488", :text=>"@person hi", :source=>"web", :truncated=>false, :in_reply_to_status_id=>nil, :in_reply_to_status_id_str=>nil, :in_reply_to_user_id=>11739102, :in_reply_to_user_id_str=>"11739102", :in_reply_to_screen_name=>"person", :user=>{:id=>1000628702, :id_str=>"1000628702", :name=>"test account", :screen_name=>"testaccountso", :location=>"", :description=>"", :url=>nil, :entities=>{:description=>{:urls=>[]}}, :protected=>false, :followers_count=>0, :friends_count=>0, :listed_count=>0, :created_at=>"Mon Dec 10 01:27:39 +0000 2012", :favourites_count=>0, :utc_offset=>nil, :time_zone=>nil, :geo_enabled=>false, :verified=>false, :statuses_count=>1, :lang=>"en", :contributors_enabled=>false, :is_translator=>false, :profile_background_color=>"C0DEED", :profile_background_image_url=>"http://a0.twimg.com/images/themes/theme1/bg.png", :profile_background_image_url_https=>"https://si0.twimg.com/images/themes/theme1/bg.png", :profile_background_tile=>false, :profile_image_url=>"http://a0.twimg.com/sticky/default_profile_images/default_profile_3_normal.png", :profile_image_url_https=>"https://si0.twimg.com/sticky/default_profile_images/default_profile_3_normal.png", :profile_link_color=>"0084B4", :profile_sidebar_border_color=>"C0DEED", :profile_sidebar_fill_color=>"DDEEF6", :profile_text_color=>"333333", :profile_use_background_image=>true, :default_profile=>true, :default_profile_image=>true, :following=>nil, :follow_request_sent=>false, :notifications=>nil}, :geo=>nil, :coordinates=>nil, :place=>nil, :contributors=>nil, :retweet_count=>0, :entities=>{:hashtags=>[], :urls=>[], :user_mentions=>[{:screen_name=>"person", :name=>"Person", :id=>1173910, :id_str=>"1173910", :indices=>[0, 6]}]}, :favorited=>false, :retweeted=>false}> 

我可以像这样访问提及的文本:@allmentions[0].text

是否有内置的 ruby​​ 方法(或简单的方法)让我只存储在 text 属性中具有唯一值的提及?

4

2 回答 2

5

是的,你可以uniq用块调用。

例如:

@allmentions.uniq {|m| m.text}
于 2012-12-10T01:43:42.253 回答
0

为了回答我自己的问题,我做了一些研究,似乎这样可行:

 no_dupes = $allmentions.uniq { |h| h[:text] }
于 2012-12-10T01:45:30.347 回答