Ruby Ruby Collections Ruby Hashes Working with Hash Keys

Edward A. Polanco Murillo
Edward A. Polanco Murillo
2,749 Points

Set a new variable called food to true

Unable to figure out the second step of this practice question. Ive tried multiple answers but i am unable to get the correct output.

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

2 Answers

KRIS NIKOLAISEN
KRIS NIKOLAISEN
53,552 Points

food isn't a new item. It is a variable just like hash is with a value of true if "calories" is found and false otherwise. Since hash.has_key?("calories") returns true or false based on if "calories" is found, the result can be directly assigned to food.

food = hash.has_key?("calories")
Edward A. Polanco Murillo
Edward A. Polanco Murillo
2,749 Points

thank you again I was able to figure it out.