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

Mayank Munjal
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Mayank Munjal
Front End Web Development Techdegree Graduate 18,120 Points

Not sure what's wrong with my code!

Can't figure out the error, any help will be appreciated!

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

if grocery_item.has_value?("bread")
  grocery_item["food"] = true
end
Mayank Munjal
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Mayank Munjal
Front End Web Development Techdegree Graduate 18,120 Points

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.

Brian Gilbank
Brian Gilbank
9,586 Points

You need to replace grocery_item["food"] = true with

grocery_item.store("food", true)

1 Answer

Mayank Munjal what you're doing is correct, and you shouldn't get any errors. The only thing to notice is that Ruby is case sensitive, so "bread" is not the same as "Bread", thus in your case grocery_item.has_value?("bread") will return false.

If you do this, it will set your food key to true:

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

if grocery_item.has_value?("Bread") # notice I'm checking the exact case as in the hash above
  grocery_item["food"] = true
end

puts grocery_item["food"]

Let me know if that works out for you! ?