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

What's wrong with my code?

It keeps telling me that "food" does not appear on the grocery_item has as "true". But I'm looking at the code and I think I got it correctly. What am I doing wrong? Been staring at this code for awhile.

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

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

is "Bread", so you can also set it to true.

grocery_item["food"] = true

Use true, not the string "true". That's what treehouse excepts.

Hope you understand! ~xela888

1 Answer

first off, you don't need to ask if true is true, which is true, to run. You can delete the "== true" part as shown:

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

into

if grocery_item.has_value?("Bread")
end

second off, change

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

into

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

*** MOD Note: Moved from "comment" to "Answer" ***

Thanks Alex! Tried that code but the error is still coming up. Not sure if that's a bug here on Treehouse. :(

Okay it's weird. It worked with...

grocery_item["food"] = true