On Fri, 17 Feb 2006, Jeff C. 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:
class << a
In both cases, you’re defining a method that only the object a can
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 A. Black (email@example.com)
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