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

Yuichi Narisawa
Yuichi Narisawa
19,548 Points

Where should I put the variable "food"?

It looks so dummy, but I cannot find the answer. Where should I wright the "food" variable? I'm totally stacking... X(

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

1 Answer

Ricky Johnston
Ricky Johnston
27,445 Points

I'm new to learning Ruby as well, but I think I've got your answer here. The challenge is asking you to set food = true if the key exists.

The line that you've written:

hash.has_key? ("calories")

checks to see if the key "calories" is set in your hash... but only returns that value. You aren't doing anything with it. If you use it in an if statement:

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

you should get the result you're looking for.

Yuichi Narisawa
Yuichi Narisawa
19,548 Points

Hi, Ricky!

Thank you for taking your time to answer my question! Finally, I can move forward! Thank you so much, mate! ;)