簡體   English   中英

訪問數組內部的哈希元素

[英]Accessing a hash element inside of an array

當我嘗試運行以下方法時,我使用的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> 
...

我正在嘗試通過哈希符號訪問結果。

當我轉換.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,
...
}

您正在處理Tweet 數組

您可以通過遍歷Array來訪問每個Tweet中的特定符號:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM