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

4Ctech Admin
4Ctech Admin
5,062 Points

Challenge Task 2 not working

The question asks to create an array called grocery_list and to place the hash value with the "item" key in it.

I tried running this in irb and its working fine, but it doesn't get accepted in the challenge itself:

grocery_list = [] grocery_list[0] = grocery_item.values_at("item")

hash.rb
grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company" }
if grocery_item.has_value?("Bread")
  grocery_item["food"] = true
end 
grocery_list = []
grocery_list[0] = grocery_item.values_at("item")
4Ctech Admin
4Ctech Admin
5,062 Points

Never Mind. I realized that the values_at function returns an array So all that has to be done is to set grocery_list = grocery_item.values_at("item")

2 Answers

ARNOLD SANDERS
ARNOLD SANDERS
5,266 Points
grocery_item = {
   "item" => "Bread", 
   "quantity" => 1, 
    "brand" => "Treehouse Bread Company" }

grocery_list = [ ]
grocery_item.each do |k, v|
    grocery_list << k if k == 'item'
end
grocery_list

thats how you would input only the key into the new array. if I'm understanding the problem right.

I can't read