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

I was told on a code challenge to "Using the has_key? method, check if the hash variable has a key called "calories". I"

When I was told to do this I in turn put:

hash.has_key?("calories")

however I am not sure where to go from here because I don't know how and where I should define the variable and everything that I try does not work.

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

1 Answer

Angela Visnesky
Angela Visnesky
20,927 Points

Hi Ellis! You need to set this challenge up as an if statement.

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

Keep learning!

Hey, Angela!

Thanks so much for the help! It's funny, I didn't even think about an if statement because the module was just focused on hash keys. that was quite a curve ball. But thank you so much for reaching out and helping me so promptly.