Ruby Ruby Collections Ruby Hashes Working with Hash Keys

Brant Faulkner
Brant Faulkner
7,902 Points

Stuck on Stage 2 of Ruby Hashes

My current code is trying to make anther key and value.

I have also tried the second line of the if statement: food = true "food" = "true" etc.

I am not sure what it wants me to do.

Thanks

hash.rb
hash = { "name" => "Bread", "quantity" => 1, "calories" => 100 }
If
hash.has_key?("calories")
hash["food"] = "true"
end

1 Answer

Maciej Czuchnowski
Maciej Czuchnowski
36,429 Points

Well, the new code should look something like this:

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

Notice that if keyword should be in the same line as the condition and food needs to be just a variable, not a key in the hash - "set a new variable called "food" to true" - also, names of variables should not be in double quotes, same goes for boolean values.

Brant Faulkner
Brant Faulkner
7,902 Points

Thanks for the assistance. The explanation was helpful.