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

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.