Ruby Ruby Collections Ruby Hashes Working with Hash Keys

Yashesh Shah
PRO
Yashesh Shah
Pro Student 2,426 Points

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

why am I getting an error?

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

1 Answer

Steven Parker
Steven Parker
176,321 Points

The instructions want you to set a new variable called food to true, but only if the hash variable has a key called "calories".

This code tests for the value but always sets the variable to true. To make it conditional, you could either create a conditional ("if") block around the assignment, or assign the variable directly using the test result.

And you won't need to output anything.