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

Trying to store the key "food" in my hash if the statement = true

Not sure what I'm doing wrong here.

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

2 Answers

Martin Cornejo Saavedra
Martin Cornejo Saavedra
18,132 Points

First, the string is case sensitive, in the has_value? use "Bread" with big B. You have to add a new key,value pair to the existing hash, you do that by doing this:

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

Thank you, I was struggling with this one. I forgot that t was case sensitive. Also adding the value to the hash I had correct the first time, I changed it to just adding a value because I thought that's where the problem was.

I tried the code you displayed and it keeps telling me it's not find food in the hash?

Faisal Fehad
Faisal Fehad
1,582 Points

I guess this is what you trying to do?

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

if grocery_item.has_value?"bread".capitalize grocery_item['item'] = "food" end