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

John Coolidge
John Coolidge
12,614 Points

Error in Working with Hash Keys exercise

I'm not sure how to solve this exercise. I assume that checking the hash variable for the calories key and if so add a new key called "food" and set it to true would look like my code below.

When I submit my answer, I get "The food variable was not found".

I'm not a newb to programming in general, but perhaps I've missed something here. We just covered the .store method and if statements were covered a while back. What else could I need to check if a key exists and if so create another with a Boolean value?

hash.rb
hash = { "name" => "Bread", "quantity" => 1, "calories" => 100 }
if hash.has_key?("calories")
  hash.store("food", true)
end
John Coolidge
John Coolidge
12,614 Points

I've also typed the above into IRB and it works without a problem.

1 Answer

Hi John,

The challenge is not asking you to create a new hash key called "food." The challenge simply asks you to create a new variable called food. So instead of using

hash.store("food", true)

type

def food = true

Hope this helps!

John Coolidge
John Coolidge
12,614 Points

Wow, do I feel a little silly. Thanks so much, Zach! I was overdoing it. :)