Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby

Christopher Quinn
Christopher Quinn
9,943 Points

Module - Extend

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

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

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

hello.say_hello

With these I keep getting the error " 'say_hello': undefined local variable or method 'name' for #<Hello:(HEXCODE) @name="Chris"> (NameError). Any ideas why it isn't working? It apparently works in the video "Extend and Include Part 1".

1 Answer

You need to include the "@" symbol in your module SayHello on the puts "Hello #{name}" line

Your line should look like this: puts "Hello #{@name}"

Your code should work then.

Christopher Quinn
Christopher Quinn
9,943 Points

That's what I get for trying to do this on no sleep, didn't even realize I forgot it. Always helps to have a second set of eyes when you make a silly mistake :). Thanks!