Ruby Ruby Collections Ruby Hashes Working with Hash Keys

Harsh Mankad
Harsh Mankad
4,650 Points

Question is not clear

The 2nd part of the question where it's asking to set a new variable called "food" to "true" is not clear whether it means adding it to the hash or creating it separately.

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

2 Answers

Steven Ang
Steven Ang
40,658 Points

The has_key? method returns a boolean value which means it's either true or false if the argument given, in this case, calories is in the hash. For this challenge, you are given the task of storing the value of hash.has_key("calories") into a new variable named food. The value of it will be true because the key calories is in fact in the hash.

food = hash.has_key?("calories")