Ruby Ruby Collections Ruby Hashes Working with Hash Keys

Henry Dashwood
Henry Dashwood
Pro Student 4,336 Points

Help with setting a new variable to true

I'm fairly sure I have the correct answer to the first part of this question:


It's the second part that I'm less sure about. The wording of the question suggests using an "if" statement however I think this might be a mistake on my part.

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

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

2 Answers

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

You're very close here. But in your code you are asking if the string "food" is equal to true. Somewhere in there you got the assignment and equality operators mixed up. And "food" in your code is simply a string, not a variable name. Take a look at my solution and see how close you are!

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

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

Note that I left off the == true from the first line with "calories" as it's not really needed, but you can keep it. Either is valid.