From: dblack@... Date: 2006-02-17T01:56:00+09:00 Subject: Re: Newbie: how to dynamically add methods? Hi -- On Fri, 17 Feb 2006, 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. The "<< obj" notation takes you into the singleton class of obj, which is where that particular object's singleton methods (i.e., those that only that object can call) are stored. Except for some minor differences that usually don't matter, these two things are equivalent: def a.meth end and class << a def meth end end In both cases, you're defining a method that only the object a can call. > Anybody know of a good book or online tutorial for this kind of thing? http://www.rubygarden.org/ruby?SingletonTutorial goes over singleton classes and methods, and also the other meaning of "singleton" in Ruby (not closely related, except by coincidence of name, but good to know about *because* of the coincidence of name). David -- David A. Black (dblack@wobblini.net) Ruby Power and Light (http://www.rubypowerandlight.com) "Ruby for Rails" chapters now available from Manning Early Access Program! http://www.manning.com/books/black