Ruby Ruby Collections Ruby Hashes Working with Hash Keys

hash.has_key?("calories") hash["food"]= "true" Why isn't this correct?

I don't understand what I"m missing here for this code challenge. ???

hash = { "name" => "Bread", "quantity" => 1, "calories" => 100 }

hash["food"] = "true"

2 Answers

Samuel Ferree
Samuel Ferree
31,717 Points

You want to put your call to "has_key?" in an If statement, so that you only create the new variable if the hash has the key "calories"

You need to make a new variable called "food", not a new key in the hash

and you need to set the value of "food" to the boolean value true, not the string value "true".

The instructions are kind of confusing, here's my solution.

hash = { "name" => "Bread", "quantity" => 1, "calories" => 100 }

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

10-4 good buddy. Thank you for the help!