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 Ruby Hash Creation

Problem with Create a key in the grocery_item_1 hash called "name" and set the value of that key to the string "milk".

I am completely new to programming and started doing Ruby course, from my understanding the code I used is correct but it still fails and I do not understand why.

grocery_item_1 = { "name" => "milk" } and grocery_item_1 = { name: "milk" }

what is wrong with the above and what are key differences to know?

hash.rb
grocery_item_1 = { name: "milk" }

What error are you getting?

I think you are getting an error because you didn't add it on to your code from task 1, where you create a new hash and assign it to a variable called grocery_item_1

1 Answer

Hey, thank you for an answer. Actually the reason the one code passes and the other fails lays in the question, we are to asign value in the form of the string to the key name. When declaring key: value it has the form of a symbol so it doesn't work, if we do it "key" => "value" it is a string. so although ruby grocery_item_1 = { name: milk } is the same as ruby grocery_item_1 = { 'name' => 'milk' }

in the first case it is as symbol and only with => we declare it as a string.