Ruby Ruby Collections Ruby Hashes Working with Hash Keys

Henry Dashwood
PRO
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:

hash.has_key?("calories")

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.rb
hash = { "name" => "Bread", "quantity" => 1, "calories" => 100 }

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

2 Answers

Jennifer Nordell
STAFF
Jennifer Nordell
Treehouse Staff

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
end

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.