Ruby Ruby Collections Ruby Hashes Working with Hash Keys

I was told on a code challenge to "Using the has_key? method, check if the hash variable has a key called "calories". I"

When I was told to do this I in turn put:

hash.has_key?("calories")

however I am not sure where to go from here because I don't know how and where I should define the variable and everything that I try does not work.

hash.rb
hash = { "name" => "Bread", "quantity" => 1, "calories" => 100 }
hash.has_key?("calories")
food = "bread"
hash.store(food => "bread")

1 Answer

Angela Visnesky
Angela Visnesky
20,926 Points

Hi Ellis! You need to set this challenge up as an if statement.

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

Keep learning!

Hey, Angela!

Thanks so much for the help! It's funny, I didn't even think about an if statement because the module was just focused on hash keys. that was quite a curve ball. But thank you so much for reaching out and helping me so promptly.