From: s@... (Stefan Schmiedl) Date: 2002-04-10T10:32:39+09:00 Subject: Re: class << self question On Wed, 10 Apr 2002 00:20:05 +0200, Markus Jais wrote: > hello > > in several libraries I found this: > class << self > > now I played with it and generated the following sample > code (does not do anything useful, just for playing with the concept) > > class Vector > def initialize (x, y) > @x = x > @y = y > end > here self refers to class Vector > class << self > def func > puts "in func" > end > end > > def my_method when my_method is evaluated self refers to an instance of class Vector > self.func() # Error!! why does this not work ??? > end > > end > > I understand that > class << self > in this case generates a class method called "func" with class << self you are introducing an anonymous super class whose methods are available for lookup > > but I also thought that > class << object > extends an object > (like in the pickaxe book on page 243) by exactly the same mechanism as above. > > why does this not work with > class << self ?? > why can I not call > self.func() in the method "my_method" above ?? because during the definition self is Vector and during execution self is an instance of Vector. > > maybe I am just doing something wrong. - meditate over the pictures in the pickaxe book, esp. p. 246 - sprinkle your source with "p self" in strategically important places sorry for not being more clear here, but the last time I tried to explain this stuff, a reviewer recommended to leave it out (hi clemens ;->). s. -- Stefan Schmiedl Approximity GmbH http://www.approximity.com Research & Development mailto:stefan@approximity.com Loreleystr. 5, 94315 Straubing, Germany Tel. (0 94 21) 74 01 06, Fax (0 94 21) 74 01 21 shhhh ... I can't hear my code!