Ruby Ruby Collections Ruby Hashes Working with Hash Keys

John Coolidge
John Coolidge
12,613 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,613 Points

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

1 Answer

Zach Stearman
Zach Stearman
8,444 Points

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,613 Points

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