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

Stuck on: Ruby Collection Challenge Task 1

Using the has_key? method, check if the hash variable has a key called "calories". If it does, set a new variable called food to true.

I think the part "food == true" is fault. But I can't figure out how. Please help! :(

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

3 Answers

  • You're checking to see if there's a "calories" key, but youre not instructing your program to do something if it has this key.
  • On line 3 you're checking to see if food is equal to true, but you havent defined a food variable in your script.

Let me know if that helps here

Hey, There is not that much wrong with your code, all you have to do is put a "=" instead of the "==" and put the has_key? method inside an if statement.

Good luck

Thank you for the advice! :D solved it!