Ruby Ruby Foundations Modules Extend and Include: Part 2

I am having trouble getting the code to work in Linux Ubuntu for the first lesson in the Extend and Include lesson.

When I run the program I get this error on both the extend and the include programs.

This program does not work because it has a undefined local variable or method 'name for #<Hello:0x00000....... @name=”Bob”> (NameError).

Here is the code for the Extend program:

class Hello
    def initialize(name)
        @name = name
    end
end

module SayHello
    def say_hello
        puts "Hello #{name}"
    end
end

hello = Hello.new("Bob")
hello.extend SayHello

hello.say_hello

2 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,429 Points

Put this in line 9 instead of what you have:

puts "Hello #{@name}"

Watch the video again, it's around 0:28.

Thank you! That was what I was missing. I looked over the code many times and missed that symbol every time.

Bob