Ruby Ruby Collections Ruby Hashes Working with Hash Keys

Yuichi Narisawa
Yuichi Narisawa
19,548 Points

Where should I put the variable "food"?

It looks so dummy, but I cannot find the answer. Where should I wright the "food" variable? I'm totally stacking... X(

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

1 Answer

Ricky Johnston
Ricky Johnston
27,445 Points

I'm new to learning Ruby as well, but I think I've got your answer here. The challenge is asking you to set food = true if the key exists.

The line that you've written:

hash.has_key? ("calories")

checks to see if the key "calories" is set in your hash... but only returns that value. You aren't doing anything with it. If you use it in an if statement:

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

you should get the result you're looking for.

Yuichi Narisawa
Yuichi Narisawa
19,548 Points

Hi, Ricky!

Thank you for taking your time to answer my question! Finally, I can move forward! Thank you so much, mate! ;)