Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Collections Ruby Hashes Working with Hash Values

values_at method for grocery item is not correct. What am I doing wrong?

I've tried this code a dozen few way including setting the grocery_list = Array.new and adding in the key and value. Nothing seems to work. I just need to know where I'm going wrong.

thanks!

hash.rb
grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company" }

if grocery_item.has_value?("Bread")
  grocery_item["food"] = true
end 
grocery_item.values_at("grocery_list" => Array.new)
grocery_list["item"] = grocery_item

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hi Aaron,

The way I did this challenge (and I'm sure there are other ways) is after the 1st task, I first create an empty, new array called grocery_list. I then used the += to add the new 'item' into the new array.

Hope this helps and makes sense. Keep Coding! :)

grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company" }

if grocery_item.has_value?("Bread")
  grocery_item["food"] = true
end 

grocery_list = Array.new
grocery_list += grocery_item.values_at("item");