From: Wilson Bilkovich Date: 2006-02-17T00:49:06+09:00 Subject: Re: Newbie: how to dynamically add methods? On 2/16/06, Jeff Cohen wrote: > The PickAxe doesn't seem to cover details of how to dynamically create > class or add methods to existing classes. For example, sometimes I see > people doing things like this: > > class << self > ... > > and I'm wondering exactly what's going on. > > Anybody know of a good book or online tutorial for this kind of thing? > This may help: http://www.whytheluckystiff.net/articles/seeingMetaclassesClearly.html class << self is usually used for things that are a bit beyond a basic method definition. For example, if you just want to define a new class method called blah, you could write: def self.blah .... end However, if you want to do alias_method on a class method, you can't do: alias_method :self.blah, etc, etc. You need to put it inside a class << self section.