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:in `display'
  from (irb):11
  from /usr/bin/irb:12:in `<main>'

···

--
Posted via http://www.ruby-forum.com/\.

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.

···

--
Posted via http://www.ruby-forum.com/.

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:in `display'
  from (irb):22
  from /usr/bin/irb:12:in `<main>'

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

···

--
Posted via http://www.ruby-forum.com/\.

watch this: http://www.youtube.com/watch?v=iLxKNUFHAnY

···

On Tue, Feb 26, 2013 at 12:05 PM, Love U Ruby <lists@ruby-forum.com> wrote:

>> 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:in `display'
  from (irb):11
  from /usr/bin/irb:12:in `<main>'

--
Posted via http://www.ruby-forum.com/\.

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.

···

--
Posted via http://www.ruby-forum.com/\.

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.

···

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

>> 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:in `display'
  from (irb):11
  from /usr/bin/irb:12:in `<main>'

--
Darryl L. Pierce <mcpierce@gmail.com>
http://mcpierce.multiply.com/
"What do you care what people think, Mr. Feynman?"

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.

···

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?

--
<https://github.com/stomar/&gt;