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

Jarmo Pesonen
Jarmo Pesonen
2,607 Points

Hi, I am stuck. What is wrong with following code? I am just getting food variable not found response

I have not added "food" => false information to first line but if I understand this correctly it is not supposed to be there and I if I put it there I still get same response

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

if hash.has_key?("calories") == true 
  hash.store("food",true) 
end

3 Answers

Cristian Altin
Cristian Altin
12,165 Points

Basically the if clause checks if there's a key named "calories" in the hash. If there's one in your case it will add a new key,value combo.

The store method changes the value of a key if it exists else it creates the specified key with its value. That is why it's indifferent to you adding "food" => false in the hash definition.

Jarmo Pesonen
Jarmo Pesonen
2,607 Points

thanks for reply.. I am still interested to know a) did I understood the task correctly b) what is needed to fix it

Cristian Altin
Cristian Altin
12,165 Points

Actually no, it asks you to create a variable called "food" not a key in the hash. ;-)

P.s. remember to set it to true as asked. You are almost there!

Jarmo Pesonen
Jarmo Pesonen
2,607 Points

haha..

I quess I was so deep in "hash world" that variable turned into hash :-)

thanks!