2012-12-10 34 views
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方法(或简单的方法)让我只存储文本属性中具有唯一值的提及?

回答

5

是的,你可以拨打电话uniq

例如:

@allmentions.uniq {|m| m.text} 
+0

的感谢!我不知道我会替代/一些文字/虽然?我只想提及独特的文字值。 – tonic

+0

我看到你现在已经修改了你的答案以符合我的建议。完善! – tonic

0

要回答我的问题,我做了一些研究,它看起来像这样的工作:

no_dupes = $allmentions.uniq { |h| h[:text] }