简体   繁体   中英

Accessing a hash element inside of an array

The gem 'twitter' I am using is providing me the following results when I attempt to run the following method:

def timeline
    @mytimeline = Twitter.user_timeline("cnn")
    newstring = @mytimeline.to_s
    puts newstring
  end

#<Twitter::Tweet:0x00000001647390>                                                                                                                                                                                                             
#<Twitter::Tweet:0x00000001647368>                                                                                                                                                                                                             
#<Twitter::Tweet:0x00000001647340> 
...

I am trying to access the results via the hash symbol.

When I convert .to_s I can get a huge dump of the entire hash but in a string...

What is the easiest way that I can access the symbols? Here are the results I get:

[#<Twitter::Tweet:0x0000000210ba88 
@attrs={
:created_at=>"Thu Sep 19 17:01:56 +0000 2013", 
:id=>380738540914479104, 
:id_str=>"380738540914479104", 
:text=>"What if you could buy a smartphone that would last you for the rest of your life? http://t.co/3j5N9gz18H", 
:source=>"web", 
:truncated=>false, 
:in_reply_to_status_id=>nil, 
:in_reply_to_status_id_str=>nil,
...
}

You're dealing with a Array of Tweet s.

You can access a certain symbol in each Tweet by iterating through the Array :

@mytimeline.each { |tweet| print tweet[:text] }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM