>> 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>'
on 2013-02-26 18:05
on 2013-02-26 18:20
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.
on 2013-02-26 18:27
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?
on 2013-02-26 18:37
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.
on 2013-02-26 19:58
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 2013-02-26 21:53
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:in `display' > from (irb):11 > from /usr/bin/irb:12:in `<main>' 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.
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.