Ruby Ruby Collections Ruby Hashes Working with Hash Values

Mayank Munjal
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
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,585 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! ?