Undefined local variable or method `m'

class Test
m=self
def self.show
print “hi”
end
def display
m.show
end
end
=> nil

Why does the below error come? can anyone help me here?

foo = Test.new
=> #Test:0x0000000106f9b8

foo.display
NameError: undefined local variable or method m' for #<Test:0x0000000106f9b8> from (irb):7:indisplay’
from (irb):11
from /usr/bin/irb:12:in `’

Because m is an undefined local variable or method.
If you have the urge to do weird stuff like that, then make “m” a
Constant, class variable, instance variable, or method.

tried the below also:

class Test
@m=self
def self.show
print “hi”
end
def display
m.show
end
end
=> nil

foo = Test.new
=> #Test:0x00000000eb85e8

foo.display
NameError: undefined local variable or method m' for #<Test:0x00000000eb85e8> from (irb):18:indisplay’
from (irb):22
from /usr/bin/irb:12:in `’

I think something I am doing wrong,conceptually here. What is it,could
you tell me?

watch this: Ruby Programming Variable Scope - YouTube

Thanks for sharing the link. Thank you very much.

But here I finally worked it out :

foo.class
=> Test

“abc”.class
=> String

class Test
def initialize()
@m=self
end
def self.show
print “hi”
end
def display
@m.class.show
end
end
=> nil

foo = Test.new
=> #<Test:0x0000000121a150 @m=#<Test:0x0000000121a150 …>>

foo.display
hi=> nil

But had a confusion with my first example. For that I have too see that
you-tube link.

Am 26.02.2013 18:28, schrieb Love U Ruby:

I think something I am doing wrong,conceptually here. What is it,could
you tell me?

Yes, I already told you only a couple of days ago:

You do not have the slightest idea how to properly define
a class with an instance variable.
My students learn that in their first two weeks.

Maybe a metaphor helps: it’s like you are trying to read
Shakespeare but you don’t even know 50 words of English.
So you have to ask us for the meaning of 9 out of 10 words
and still don’t get the meaning of the whole sentence,
let alone the complete play.

That sucks.

Learn the basics. You got some good references from us.

On Wed, Feb 27, 2013 at 02:05:07AM +0900, Love U Ruby wrote:

Why does the below error come? can anyone help me here?

foo = Test.new
=> #Test:0x0000000106f9b8

foo.display
NameError: undefined local variable or method m' for #<Test:0x0000000106f9b8> from (irb):7:indisplay’
from (irb):11
from /usr/bin/irb:12:in `’

Since your variable is m and not @m (instance scope) or @@m (class
scope) then it doesn’t exist when the display function is called.