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

Am I adding the new key to the hash correctly?

I'm not sure if I'm using the method for adding a new set of key/value to an existing hash that the challenge wants.

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

2 Answers

The challenge wants you to set the value to the boolean true not a string.

Try getting rid of the double quotes around the true value :)

Good luck! ~alex

Tried that (as that's what I then next thought) and got " Bummer! The key food was not found in the grocery_item hash." So hm.

True should be a boolean and not a string so remove them double quotes.

There are a few ways of adding a new Key -> Value pair to a hash and what you have done isn't wrong, but the test needs you to assign the key as a string to get it to pass.

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

This should pass. Its worth noting that you can use strings or symbols as hash keys. There is a difference and its worth researching.

Read here for more info....

strings or symbols?