Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Collections Ruby Hashes Working with Hash Keys

Henry Dashwood
PLUS
Henry Dashwood
Courses Plus 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
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
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
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.