From: Yossef Mendelssohn Date: 2008-10-08T03:48:27+09:00 Subject: Re: class << self On Oct 7, 11:53 am, "Patrick Doyle" wrote: > Would it be fair to say that 99% of the time there is no difference between > the definitions of A#a, A#b, and A#c? > > class A >   def self.a >     puts "self.a" >   end > >   def A.b >     puts "A.b" >    end > >   class << self >     def c >       puts "A.c" >     end >   end > end > > It seems that the construct: > > class << self >   def x >   end > >   def y >   end > >   def z >   end > end > > might be used primarily as a typing shortcut and that 99% of the time that's > all it is. Personally, I use class << self for several reasons: - It reduces repetition - I believe it's clear enough - It makes searching for a method definition in your entire codebase simpler - It allows aliasing class methods - It allows access control (public/private) of class methods With that in mind, I see def self.meth as a shortcut for class << self when all you're doing is defining a single method. If anything more is to be done, it's cleaner overall to enter the eigenclass. And I use self instead of the literal class name again to reduce repetition. If you have class A def A.a 'got to .a' end end and you decide to change the class name from A to something else, you're still defining A.a, which is now highly likely to be an error. If you were using self.a or class << self, the method would still be defined on whatever the class is now called. -- -yossef