0

Ruby はまったく初めてで、ドキュメントを調べても、探しているものが見つからないようです。オブジェクトがあり、それを掘り下げて何かにアクセスしようとしています。オブジェクト tweets[0] は次のようになります...

--- !ruby/object:Twitter::Tweet
attrs:
  :created_at: Wed Apr 10 00:58:21 +0000 2013
  :user:
    :location: ''
    :entities:
      :description:
        :urls: []
    :protected: false
  :geo:
  :entities:
    :hashtags:
    - :text: adult
      :indices:
      - 34
      - 40
    :urls: []
    :user_mentions: []
    :media:
      :indices:
      - 41
      - 63
      :url: http:t.co/i-need-this-image
      :type: photo
      :sizes:
        :thumb:
          :w: 150
          :h: 150
          :resize: crop
        :small:
          :w: 340
          :h: 453
          :resize: fit
        :medium:
          :w: 600
          :h: 800
          :resize: fit
        :large:
          :w: 768
          :h: 1024
          :resize: fit

さまざまな方法を試しましたが、どれも正しく機能していないようです。それらを捨てるために私は使ってきました

puts YAML::dump(tweets[0])
--
puts YAML::dump(tweets[0].media) # returns the media method correctly
puts YAML::dump(tweets[0]['media']) # also seems to do it
puts YAML::dump(tweets[0].media.url) # idk
puts YAML::dump(tweets[0]['media']['url']) # I feel like this should work but it doesn't
4

1 に答える 1

1

以下は私のために働いた:

require 'yaml'
tweets = YAML.load_file('test.yml') # this file contains a copy of the YAML
p tweets["attrs"][:entities][:media][:url] # "http:t.co/i-need-this-image"
于 2013-04-15T05:49:38.203 に答える