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

Alphonse Cuccurullo
Alphonse Cuccurullo
2,513 Points

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

Someone please help im so confused. =(

2 Answers

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

# checking if the hash grocery_item has a value of bread
if grocery_item.has_value?("Bread")

   # if the hash grocery_item does have bread 
   # make a new key named food and set it to true
   grocery_item["food"] = true

end

#value stored in the array will be the value of the item in grocery_item hash

grocery_list = [grocery_item.values_at('item')]

#grocery_list = ["Bread"]
Alphonse Cuccurullo
Alphonse Cuccurullo
2,513 Points

but what about the values_at method how i use that?

Hey Alphonse,

The second the last line of Ethan's code uses the values_at method to return the value of the key 'item', which in this case is "Bread". The hash structure is like this:

"key" => "value"

Check out the Ruby Docs here for more on how hashes and their methods work: http://ruby-doc.org/core-2.2.0/Hash.html