Ruby Ruby Collections Ruby Hashes Ruby Hash Creation

imanewbie
imanewbie
9,138 Points

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" }
K .
K .
2,356 Points

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

imanewbie
imanewbie
9,138 Points

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.