2013-04-15 2 views
0

루비를 처음 접했을 때 문서를보고 내가 찾고있는 것을 찾지 못하는 것 같습니다. 나는 물건을 가지고 있고 그것에 접근하기 위해 그것을 파 내려고하고있다. 객체 트윗 [0]은 다음과 같이 보입니다.Ruby에서이 값에 어떻게 액세스합니까?

--- !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 

답변

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" 
+0

아, 그게 내가 찾던 구문입니다. 그것은 [: media]까지 작동합니다. 그런 다음 [: url]을 추가하면 오류가 발생합니다. => Liquid Exception : Symbol을 index.html의 정수로 변환 할 수 없습니다. – coryj

+0

이렇게하면 이상합니다 :'YAML :: 덤프 (짹짹 [ "attrs"] [: 엔티티] [: 미디어] [: url])'나는 이것을 얻는다 :' "--- http : t.co/i-need-this-image \ n ... \ n ". – fmendez

+0

그래서, 결국 나를 위해 일했다. 나는 왜, 또는 어떻게 ... 그러나 그것이 작동하는지에 관해 모른다. "짹짹 [i] .media [0] .url' – coryj

관련 문제