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

Dmitriy Kavyazin
Dmitriy Kavyazin
15,624 Points

Stuck on this question, what am I doing wrong?

Using the has_value? method, check to see if the grocery_item hash has a value called "Bread". If it does, set a new key in the hash called "food" with the value of true.

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

5 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

I'm not sure what hash means in your example. Also, true should be a boolean, not a string.

if grocery_item.has_value?("Bread")
  grocery_item.store("food", true)
end
Dmitriy Kavyazin
Dmitriy Kavyazin
15,624 Points

Oh boy. Noob mistake. Thanks.

Niristotle Okram
Niristotle Okram
2,000 Points

Why doesn't it like this grocery_item.store("food", "true")

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

"true" is a string, while true is a boolean value.

Naya Moss
Naya Moss
9,894 Points

Thanks @Maciej Czuchnowski but, my issue with this is how did you know to use .store? When I tried to use .company, that didn't work.

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Naya, Jason uses this method in one of the previous videos:

http://teamtreehouse.com/library/ruby-collections/ruby-hashes/working-with-hash-keys

around 4:08, as well as in the teacher's notes under that video. There is no built-in method .company in Ruby as far as I know.

Tome Perica
Tome Perica
14,604 Points

Task 1

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

Task 2

Just add this line of code

grocery_list = grocery_item.values_at("item")

What are these hashes? I don't understand it very well, do you have some other interactive tutorials on hashes in Ruby? :) Thanks

Daria Khaylova
Daria Khaylova
5,945 Points

Challenge Task 2 of 2

Use the values_at method on the grocery_item hash to get an array consisting of a single value: the value of the "item" key. Store the returned array in a variable named grocery_list:

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

grocery_list = grocery_list.values_at("item")

And it says 'Oops! It looks like Task 1 is no longer passing.' Why?

You are using the new variable "grocery_list" in the definition. Change "grocery_list.values_at("item")" to "grocery_item.values_at("item")