我有宝石工作:可以从它推特和拉信息。
我可以获取这个散列并打印出它的.text部分,但是我不知道如何获取id-str和screen-name-info,我真的需要解析出来。
不知道我做错了什么。

  <% Twitter.mentions_timeline.each do |mention| %>
      <%= mention.text %> <%# dies on some of em, like mention.id_str - not sure why %>
      <br /><hr />
  <% end %>

以下是twitter.inspections_timeline散列:
[#<Twitter::Tweet:0x007fd070de24a0




@attrs={
    :in_reply_to_status_id_str=>"265276218855809024",
    :retweet_count=>0,
    :contributors=>nil,
    :truncated=>false,
    :text=>"@doodleboots http://t.co/VJOR0MgQ",
    :in_reply_to_user_id_str=>"119208666",
    :geo=>nil,
    :retweeted=>false,
    :possibly_sensitive=>false,
    :in_reply_to_screen_name=>"doodleboots",
    :id_str=>"265276345704124416",
    :entities=>{:hashtags=>[],
        :urls=>[{:display_url=>"is.gd/S3uOyu",
        :indices=>[13, 33],
        :expanded_url=>"http://is.gd/S3uOyu",
        :url=>"http://t.co/VJOR0MgQ"}],
        :user_mentions=>[{:name=>"doodle",
        :indices=>[0, 12],
        :screen_name=>"doodleboots",
        :id_str=>"119208666",
        :id=>119208666}]},
        :in_reply_to_user_id=>119208666,
        :in_reply_to_status_id=>265276218855809024,
        :created_at=>"Mon Nov 05 02:16:24 +0000 2012",
        :place=>nil,
        :user=>{:profile_image_url_https=>"https://si0.twimg.com/profile_images/2808322935/d5da93a06dbd45959cb4dba0797689a5_normal.jpeg",
        :id=>926557182,
        :profile_background_image_url=>"http://a0.twimg.com/images/themes/theme1/bg.png",
        :entities=>{:description=>nil},
        :screen_name=>"Roslyn1bdzn",
        :profile_link_color=>"0084B4",
        :default_profile_image=>false,
        :created_at=>"Mon Nov 05 01:17:20 +0000 2012",
        :contributors_enabled=>false,
        :friends_count=>0,
        :time_zone=>nil,
        :favourites_count=>0,
        :utc_offset=>nil,
        :profile_use_background_image=>true,
        :url=>nil, :geo_enabled=>false,
        :lang=>"en",
        :profile_image_url=>"http://a0.twimg.com/profile_images/2808322935/d5da93a06dbd45959cb4dba0797689a5_normal.jpeg",
        :profile_text_color=>"333333",
        :name=>"Roslyn Montalbo ",
        :profile_background_image_url_https=>"https://si0.twimg.com/images/themes/theme1/bg.png",
        :description=>"Amateur social media scholar. Friend of animals everywhere. Explorer. Unapologetic twitter advocate. Incurable travel geek. Writer",
        :protected=>false,
        :profile_sidebar_border_color=>"C0DEED",
        :followers_count=>0,
        :default_profile=>true,
        :profile_background_tile=>false,
        :location=>"Dublin",
        :profile_sidebar_fill_color=>"DDEEF6",
        :following=>false,
        :statuses_count=>3,
        :follow_request_sent=>false,
        :listed_count=>0,
        :id_str=>"926557182",
        :profile_background_color=>"C0DEED",
        :notifications=>false,
        :verified=>false,
        :is_translator=>false},
        :coordinates=>nil,
        :source=>"web",
        :favorited=>false,
        :id=>265276345704124416}>,



#<Twitter::Tweet:0x007fd070de2478

    @attrs={:in_reply_to_status_id_str=>nil,
    :retweet_count=>0,
    :contributors=>nil,
    :truncated=>false,
    :text=>"test post @doodleboots",
    :in_reply_to_user_id_str=>nil,
    :geo=>nil,
    :retweeted=>false,
    :in_reply_to_screen_name=>nil,
    :id_str=>"247909685582233600",
    :entities=>{:hashtags=>[],
        :urls=>[],
        :user_mentions=>[{:name=>"doodle",
            :indices=>[10, 22],
            :screen_name=>"doodleboots",
            :id_str=>"119208666",
            :id=>119208666}]},
            :in_reply_to_user_id=>nil,
            :in_reply_to_status_id=>nil,
            :created_at=>"Tue Sep 18 04:07:29 +0000 2012",
            :place=>nil,
            :user=>{:profile_image_url_https=>"https://si0.twimg.com/profile_images/728693845/doodleBootsIcon_normal.png",
            :id=>119208666,
            :profile_background_image_url=>"http://a0.twimg.com/images/themes/theme1/bg.png",
            :entities=>{:description=>{
                :urls=>[]},
                :url=>{:urls=>[{
                    :display_url=>nil,
                    :indices=>[0, 22],
                    :expanded_url=>nil,
                    :url=>"http://doodleboots.com"}]}},
                    :screen_name=>"doodleboots",
                    :profile_link_color=>"0084B4",
                    :default_profile_image=>false,
                    :created_at=>"Wed Mar 03 00:09:50 +0000 2010",
                    :contributors_enabled=>false,
                    :friends_count=>16,
                    :time_zone=>nil,
                    :favourites_count=>0,
                    :utc_offset=>nil,
                    :profile_use_background_image=>true,
                    :url=>"http://doodleboots.com",
                    :geo_enabled=>false,
                    :lang=>"en",
                    :profile_image_url=>"http://a0.twimg.com/profile_images/728693845/doodleBootsIcon_normal.png",
                    :profile_text_color=>"333333",
                    :name=>"doodle",
                    :profile_background_image_url_https=>"https://si0.twimg.com/images/themes/theme1/bg.png",
                    :description=>"Take a pic of a drawing/doodle and tweet it @doodleboots! It will be re-tweeted and posted to the website for all to see.",
                    :protected=>false,
                    :profile_sidebar_border_color=>"C0DEED",
                    :followers_count=>14,
                    :default_profile=>true,
                    :profile_background_tile=>false,
                    :location=>"",
                    :profile_sidebar_fill_color=>"DDEEF6",
                    :following=>false,
                    :statuses_count=>16,
                    :follow_request_sent=>false,
                    :listed_count=>1,
                    :id_str=>"119208666",
                    :profile_background_color=>"C0DEED",
                    :notifications=>false,
                    :verified=>false,
                    :is_translator=>false},
                    :coordinates=>nil,
                    :source=>"web",
                    :favorited=>false,
                    :id=>247909685582233600}>,

最佳答案

它之所以适用于:text是因为您的mention散列有一个名为:text的键,而您要查找的其他属性(:screen_name:id_str)则深深地嵌套在其中。
如果仔细观察mention的结构,您会注意到它包含键:entities(与:text处于同一级别),其值是另一个包含键:user_mentions的散列。:user_mentions的值是散列数组。你要找的是在这个散列数组中。

puts mention[:entities][:user_mentions][0][:screen_name] #=> doodleboots
puts mention[:entities][:user_mentions][0][:id_str]      #=> 119208666

07-24 18:37
查看更多