Ruby code provided not valid in the course Ruby 2.0

premium
support

#1

Hello,
This refers to the course Ruby 2.0. Lesson 4, Step 3: Hashes
The instructor is teaching us to create a nested Hash @5:46

Our hash is as follows:
homer = {name: "Homer Simpson", job: "Nuclear Safety Inspector", children: ["Bart", "Lisa", "Maggie"]}

But he is telling us to do the following to create a nested hash. Here are his instructions I downloaded from the zip file:
You can add new values to a hash by writing the key that you want to add, and placing it equal to its value:

homer[:wife] = marge

I am getting an error: undefined local variable or method `marge' for main:Object

It is supposed to give us this result:
{:name => "Marge Simpson"}

Can anyone help cause it's not working. Thanks!


#2

As the error message suggests, is "marge" supposed to be a variable, method, or a String?


#3

It's very confusing because the instructor only created a hash called homer like so:
homer = {name: "Homer Simpson", job: "Nuclear Safety Inspector", children: ["Bart", "Lisa", "Maggie"]}

now he wants to create a new key value pair and writes the key wife and says he assigns it to the hash we created marge ????? The only hash we created was homer and the weird part is that his code works on the video but when I tried it, it won't work.
The result is supposed to give me a nested hash like so:

:wife => {:name => "Marge Simpson"}

marge is supposed to be the value and wife the key


#4

In this case, the value is "marge" and your example homer[:wife] = marge is assigning a variable named marge not a value - a string - "marge".

It appears your problem is that you have omitted the quotes around the name.


#5

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.