On 27 August 2010 08:33, Zack Nathan <li...@ruby-forum.com> wrote:
> Ok so I am trying to read a hash of arrays and I seem to be getting a
> cannot convert string to integer error. Here is my code:
>
>  <% @information['food']['servings'].each do |servings| %>
>  <% servings['serving'].each do |serving| %>
>    <%= serving['name'] %>
>    <br/>
>  <% end %>
> <% end %>
>
> Here is what the hash looks like:
> @parsed_response={"food"=>{"name"=>"Chicken: Breast Quarters,

I presume this is actually @information rather than @parsed_response
(or the other way around).

> rotisserie, no skin or wing (Boston Market)",
> "id"=>"42e5699f-79cd-436c-965b-90850b26a278",
> "servings"=>{"serving"=>[{"name"=>"serving (4.9 oz)",

serving is an array (the square bracket is a giveaway) so you must use
an integer to index it not 'name' as you are doing.  You can do
serving[0]['name'] or use serving.each

Colin

> "nutrients"=>{"calcium"=>nil, "satfat"=>"1", "fat"=>"4", "carb"=>"2",
> "fiber"=>"0", "sugar"=>"1", "protein"=>"33", "sodium"=>"480",
> "cholesterol"=>"85", "calories"=>"170"}}, {"name"=>"oz",
> "nutrients"=>{"calcium"=>nil, "satfat"=>"0.2025", "fat"=>"0.81",
> "carb"=>"0.405", "fiber"=>"0", "sugar"=>"0.2025", "protein"=>"6.6825",
> "sodium"=>"97.2", "cholesterol"=>"17.2125", "calories"=>"34.425"}},
> {"name"=>"g", "nutrients"=>{"calcium"=>nil,
> "satfat"=>"0.00714285714286", "fat"=>"0.0285714285714",
> "carb"=>"0.0142857142857", "fiber"=>"0", "sugar"=>"0.00714285714286",
> "protein"=>"0.235714285714", "sodium"=>"3.42857142857",
> "cholesterol"=>"0.607142857143", "calories"=>"1.21428571429"}}]}}},
> @response=#<Net::HTTPOK 200 OK readbody=true>,
> @headers={"expires"=>["0"], "last-modified"=>["Fri, 27 Aug 2010 07:30:52
> GMT"], "connection"=>["close"], "content-type"=>["application/xml"],
> "date"=>["Fri, 27 Aug 2010 07:30:52 GMT"], "server"=>["Apache"],
> "cache-control"=>["pre-check=0, post-check=0, max-age=0"],
> "pragma"=>["no-cache"], "transfer-encoding"=>["chunked"]}

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to