Ruby 访问数组中的哈希元素

Ruby 访问数组中的哈希元素,ruby,twitter,hash,Ruby,Twitter,Hash,当我尝试运行以下方法时,我使用的gem“twitter”为我提供了以下结果: def timeline @mytimeline = Twitter.user_timeline("cnn") newstring = @mytimeline.to_s puts newstring end #<Twitter::Tweet:0x00000001647390>

当我尝试运行以下方法时,我使用的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> 
...
def时间线
@mytimeline=Twitter.user\u timeline(“cnn”)
newstring=@mytimeline.to\s
放置新闻字符串
结束
#                                                                                                                                                                                                             
#                                                                                                                                                                                                             
# 
...
我正在尝试通过哈希符号访问结果

当我将.转换为_时,我可以得到整个散列的巨大转储,但在字符串中

我访问符号的最简单方法是什么?以下是我得到的结果:

[#<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,
...
}
[#“2013年9月19日星期四17:01:56+0000”,
:id=>380738540914479104,
:id_str=>“380738540914479104”,
:text=>“如果你能买一部能让你终生使用的智能手机呢?http://t.co/3j5N9gz18H", 
:source=>“web”,
:truncated=>false,
:在对状态的回复中,id=>nil,
:在对状态的回复中,id字符串=>nil,
...
}
您要处理的是一组s

您可以通过迭代访问以下内容来访问每个中的特定符号:


你想得到什么数据?打印整个Tweet对象还是只打印Tweet数据?似乎你想在Tweet集合上进行迭代。基本上,我希望能够调用任何特定的哈希符号,并对数据执行任何我想执行的操作。例如:如果我创建了一条新Tweet,我希望能够调用来查找你t新生成的tweet的twitter URL。
@mytimeline
是tweet对象的数组。因此,如果您想要第一条tweet的
文本
,请使用
@mytimeline.first.text
。要获得所有
文本
的数组,请使用
@mytimeline.map(&:text)
等等..嘿,我之前尝试过,至少我这么认为:)反正现在很好。这就是我需要前进的方向。没问题。阅读Ruby中的数组和散列,以及如何迭代它们并获取肉和土豆。是的,我一直在阅读它们。出于某种原因,我把这么简单的事情复杂化了:)
@mytimeline.each { |tweet| print tweet[:text] }