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

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!