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

Using the values_at method, create an array called grocery_list with the value of the grocery_item hash at the "item" ke

a

hash.rb
grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company" }
grocery_list = {"item" => "grocery_item"}
if grocery_item.has_value?("Bread")
  grocery_item.store("food" ,true)
end
grocery_list.values_at("item")

2 Answers

Almost, you need to call the push() method and then assign it using the values_at() method of the grocery_item to the key item.

grocery_list.push(grocery_item.values_at("item"))

Here is my code but once I put in the line of code like you have it tells me this: "Oops! It looks like Task 1 is no longer passing."

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

grocery_item.has_value?("Bread")
grocery_item["food"] = true                #adds the key "food" to the hash with a value of True

grocery_list.push(grocery_item.values_at("item"))

I even tried it with the asker's code with your code. Still says the same thing.

grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company" }
grocery_list = {"item" => "grocery_item"}
if grocery_item.has_value?("Bread")
  grocery_item.store("food" ,true)
end
grocery_list.push(grocery_item.values_at("item"))

I think I had a different bit of code at the top for my original answer to work, here is an updated solution that will work based on what you have for your code:

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

grocery_list = {"item" => "grocery_item"}

if grocery_item.has_value?("Bread")
  grocery_item.store("food" ,true)
end

grocery_list = grocery_item.values_at("item")

Awesome! That worked. Do you know why my previous code wouldn't work by chance?

The first set of code needed the if block and the second one, I think it was how I structured grocery list when I was going back to answer the question, I think I had it set as an array instead of a hash, which is why push would have worked.

It was my mistake for not pasting all of my working code.

Thanx