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

William Jones
William Jones
6,066 Points

Same Keys and values in my hashes, but the workshop says it's not set to "true". Why?

I have the exact same keys and values both my "food" hash and my "hash" hash. I've run the code in irb to double check it and it is definitely "true". Yet whenever I try to complete the worksheet it says "we changed "hash" so it didn't have a "calories" key and your "food" hash was still true. I've rewatched the video multiple times and read the teachers notes and can't find where I went wrong.

hash.rb
hash = { "name" => "Bread", "quantity" => 1, "calories" => 100 }

hash.has_key?("calories")

food = { "name" => "Bread", "quantity" => 1, "calories" => 100 } 

puts food == hash

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 27,510 Points

They want you to check if hash has a key named calories. If it does, set food to true.

See below.

Good luck on your Ruby journey it's a great language for the web, and general-purpose coding!

hash = { "name" => "Bread", "quantity" => 1, "calories" => 100 }

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