Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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! ;)