From: Robert Dober Date: 2006-04-21T18:29:29+09:00 Subject: Re: how to figure out what class a method is in for a *class method*? ------=_Part_34158_13323967.1145611765999 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On 4/21/06, Andy Gimblett wrote: > > I have a related question - a similar problem to the one just given, > but that solution doesn't fit here because I don't just want the class > name - I want an attribute associated with the class, which subclasses > can override. > > I've tried doing this using class variables, but it doesn't work: > > -- > > class Animal > @@sound =3D "nothing" > def to_s > "I say " + @@sound > end > end > > class Cat < Animal > @@sound =3D "miaow" > end > > class Dog < Animal > @@sound =3D "woof" > end > > fudge =3D Cat.new() > puts fudge > > tip =3D Dog.new() > puts tip > > -- class Animal @sound =3D "nothing" class << self attr_accessor :sound end def to_s "I say " + self.class.sound end end class Cat < Animal self.sound =3D "miaow" end class Dog < Animal self.sound =3D "woof" end fudge =3D Cat.new() puts fudge tip =3D Dog.new() puts tip That's how ruby treats it, there are some who argue that @@ should go away, maybe they are right. Hope that helps Robert > > -- > Andy Gimblett > Computer Science Department > University of Wales Swansea > http://www.cs.swan.ac.uk/~csandy/ > > -- Deux choses sont infinies : l'univers et la b=EAtise humaine ; en ce qui concerne l'univers, je n'en ai pas acquis la certitude absolue. - Albert Einstein ------=_Part_34158_13323967.1145611765999--