From: Jeremy McAnally Date: 2008-10-04T04:43:34+09:00 Subject: Re: class << self None of them are really better; they all do the same thing. :) The first and second approaches (to me) make it easier to discern, at a glance, what's a class method and what's an instance method. On the other hand, the last one (class << self) keeps you from re-typing the class name/self over and over again. --Jeremy On Fri, Oct 3, 2008 at 2:36 PM, Patrick Doyle wrote: > Sorry if this is a FAQ, but I was just looking at some Ruby code and > came across the following construct: > > class << self > def blahblahblah > ... > end > end > > I wondered what that did, went looking, and came across this email > thread from 2002: > http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/57244, in > reading that, I came across this (slightly modified) code: > > class A > B = 12 > def self.a > puts "class method A::a : #{B}" > end > > def A.b > puts "class method A::b : #{B}" > end > > class << self > B = 24 > def c > puts "class method A::c : #{B}" > end > end > > end > A.a > A.b > A.c > puts "A::B : #{A::B}" > > Could somebody enlighten me as to what is going on here? I didn't > completely follow all of the ins & outs of the thread, nor why > > def self.a > > is better/different than > > def A.a > > is better different than > > class << self > def a > > Can anybody comment or point me in the direction of some enlightenment? > > --wpd > > -- http://jeremymcanally.com/ http://entp.com/ http://omgbloglol.com My books: http://manning.com/mcanally/ http://humblelittlerubybook.com/ (FREE!)