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

Jakob Wozniak
Jakob Wozniak
17,896 Points

Bug with "Working with Hash Keys" Challenge-

There seems to be a bug with this challenge; I've tried many different ways of adding the key value pair "food" => true, but every time I get an error message saying "Food was not found".

The objective is 'Using the has_key? method, check if the hash variable has a key called "calories". If it does, set a new variable called "food" to true.'

Here's my code.

hash = { "name" => "Bread", "quantity" => 1, "calories" => 100 }

if hash.has_key?("calories")
  hash["food"] = true
end

1 Answer

Jakob Wozniak
Jakob Wozniak
17,896 Points

Wow. Turns out it wanted a brand new variable called food, not a new key-value pair. I must say, that's extremely misleading, especially since food is in quotes (like it's wanting a string). I can't imagine why you wouldn't want that as a value pair; why would you have a general variable called food?! Maybe we_have_food, or hash_has_food.

Lol, I just wasted 20 minutes on this!