From: Robert Klemme Date: 2005-08-10T00:16:09+09:00 Subject: Re: help me understand this ruby idiom: class << self; alias_method :x :y end Kelly Felkins wrote: > I'm trying to understand classes and objects. The Pick axe book > points out the "once" method in class Date and how it is defined > using the object singleton technique of "class << self". Actually > Date uses this in several places. Up until seeing Date I thought the > reason for this idiom was to add/replace methods in classes *when you > don't have convenient access to the class definition* -- basically, > use this when it's not your class you need to enhance. But the fact > that Date uses it several times indicates there is another reason to > use it. As you guessed the reasons are different: typically you use "class < class Foo irb(main):002:1> p new irb(main):003:1> end # => nil irb(main):004:0> class < p new irb(main):006:1> end TypeError: can't create instance of virtual class from (irb):5:in `new' from (irb):5 > Here are some examples from Date: > class << self; alias_method :leap?, :gregorian_leap? end > class << self; alias_method :new0, :new end > > I just noticed that every use of class << self in Date contains an > alias_method. > > Please enlighten me. Hope, I could help. Kind regards robert