From: "Ara.T.Howard" Date: 2005-08-16T03:32:37+09:00 Subject: Re: no clue On Tue, 16 Aug 2005, Joe Van Dyk wrote: > Isn't > def self.create(*a) > ... > > clearer than > > class << self > def create(*a) not when you end up using 'attr' type meta-programming, having class methods which refer to class methods, aliases, and other things that complicate the matter: harp:~ > cat b.rb class C class << self attr 'a' attr 'b' alias class_a a alias class_b b def class_initialize @a, @b = 4, 2 end def class_method puts [class_a, class_b].join('') end end self.class_initialize attr 'a' attr 'b' alias inst_a a alias inst_b b def initialize @a, @b = 'forty', 'two' end def instance_method puts [inst_a, inst_b].join('-') end end c = C::new C::class_method c.instance_method harp:~ > ruby b.rb 42 forty-two note how 'attr', 'alias', calls to methods that do not specify a receiver, and instance variables are all consistently in the class scope here. to me this is clearer than class C class << self attr 'a' attr 'b' end def self::c a + b end end i always (o.k. mostly) setup my classes like class # all constants class << self # all class stuff end # all instance stuff end if you use the the 'def self::method' approach you cannot be consistent since some things don't work that way - like 'attr' and 'alias'. for me it's just an idiom that makes my code clearer, more consistent, and avoids typing. > ? I mean, they both create a class method, right? yes. hth. -a -- =============================================================================== | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | Your life dwells amoung the causes of death | Like a lamp standing in a strong breeze. --Nagarjuna ===============================================================================