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

I know I must be missing something obvious or I would have figured it out by now, but what am I missing in my code?

Challenge 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" }
grocery_item.has_value?("Bread")
grocery_item = {"item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company", "food" => "Grain"}
grocery_item.has_value?("Grain")

5 Answers

David Axelrod
David Axelrod
36,073 Points

Just had this problem! in your if statement, after checking if grocery_item.has_value?("Bread")

you have to use the store method. it takes two arguments, the first being the key and the second being the value

HOPE THIS HELPS!!! ^_^

Chris Adamson
Chris Adamson
132,143 Points

What the challenge is looking for is an if conditional with has_value of 'Bread', and it the value exists, set the food key value to true <br>

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

Aha! I thought about that, but didn't do it. Thanks David!

Argh! I knew it was something simple and I was over thinking it. Thanks so much!!

Ok that did not work, even though I agree that your code makes sense. It gave me this error:

Bummer! SyntaxError: f1ec29fb-c5fd-4ccc-a9de-c7db85274e8e.rb:26: invalid multibyte char (US-ASCII) f1ec29fb-c5fd-4ccc-a9de-c7db85274e8e.rb:26: syntax error, unexpected end-of-input, expecting ')' if grocery_item.has_value?(β€œBread”) ^