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

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 28,720 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