I am new to Ruby, and I am having some problems with hashes.
I have XML returned from the YouTube API that I converted into a hash. Here is the hash returned by Hash.from_xml(): http://pastebin.com/9xxE6iXU
I am trying to grab specific elements from the hash for each result, such as the title, link, author, etc. Whenever I try to loop through the hash or grab a specific element, I receive a “can’t convert String into Integer” error.
Here is the code I am using for the loop:
@data["feed"]["entry"]["title"].each do |key, value|
"<p>"+key+" "+value+"</p>"
end
I have also tried grabbing specific elements, such as @data[“feed”][“entry”][“title”][0].
How do I loop through the hash and grab specific elements out?
That’s happening because
@data["feed"]["entry"]is array of hashes:Each element-hash inside this array has “id”, “category”, “title” etc. values.
For grabbing each title try to use following snippet: