Ruby Hash: Cannot Convert String to Integer

I am new to Ruby and have hash problems.

I have XML returned from the YouTube API that I converted to a hash. Here is the hash returned by Hash.from_xml (): http://pastebin.com/9xxE6iXU

I am trying to grab specific elements from a hash for each result, such as a title, link, author, etc. Whenever I try to skip a hash or capture a specific element, I get the message "can not convert String into Integer".

Here is the code I use for the loop:

@data["feed"]["entry"]["title"].each do |key, value| "<p>"+key+" "+value+"</p>" end 

I also tried to capture certain elements like @data ["feed"] ["entry"] ["title"] [0].

How do I skip a hash and capture certain elements?

+4
source share
1 answer

This is because @data["feed"]["entry"] is an array of hashes:

 puts @data["feed"]["entry"].class # => Array 

Each hash element inside this array has the values ​​"id", "category", "title", etc.

To capture each header, try using the following snippet:

 @data["feed"]["entry"].each do |entry| puts entry["title"] end # => "TABE test adult basic education" "WhatCollegesHopeYouWon'tFindOutAboutACTSATTestPrep..." .... 
+5
source

Source: https://habr.com/ru/post/1381796/


All Articles