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 Keys

Stuck on Stage 2 of Ruby Hashes

My current code is trying to make anther key and value.

I have also tried the second line of the if statement: food = true "food" = "true" etc.

I am not sure what it wants me to do.

Thanks

hash.rb
hash = { "name" => "Bread", "quantity" => 1, "calories" => 100 }
If
hash.has_key?("calories")
hash["food"] = "true"
end

1 Answer

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Well, the new code should look something like this:

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

Notice that if keyword should be in the same line as the condition and food needs to be just a variable, not a key in the hash - "set a new variable called "food" to true" - also, names of variables should not be in double quotes, same goes for boolean values.

Thanks for the assistance. The explanation was helpful.