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

Not sure why this code is not working for me

SOS I need help

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

2 Answers

Hi there,

How about something like:

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

There's probably other ways of adding to a hash, but that's the one that came to me first!

Steve.

Philip Bessa
Philip Bessa
5,396 Points

I had it like this and was stuck on it for a while:

if grocery_item.has_value?("Bread")
  grocery_item = { "food" => true }
end

The Bummer message doesn't help at all when it says the has_value? method is not being called. It says to add "food" as a key and true as the value. My code follows exactly what the videos are teaching. The challenge instruction should to be reworded so it doesn't mislead or confuse:

... create a new key in the hash called "food" and set it to true.

Doesn't your line:

grocery_item = { "food" => true }

completely overwrite the grocery_item hash? It is declared in the same way:

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

so I'm not sure you're adding to the hash, as required?

Steve.