简体   繁体   English

访问数组内部的哈希元素

[英]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: 当我尝试运行以下方法时,我使用的gem'twitter'为我提供了以下结果:

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... 当我转换.to_s时,我可以得到整个哈希表的巨大转储,但是是一个字符串...

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. 您正在处理Tweet 数组

You can access a certain symbol in each Tweet by iterating through the Array : 您可以通过遍历Array来访问每个Tweet中的特定符号:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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