On Fri, 30 Nov 2007, Pixelmech wrote:
On Nov 30, 2:11 pm, “Jeremy McAnally” [email protected]
The << indicates you’re adding class level methods rather than instance level.>
But with the << operator, you can’t call it on an instance.
So you’re essentially added a protected method? Would that be correct?
(or is it private?) So the method could only be used by other methods
withing the class?
class << obj
opens up a class definition block for the “singleton class” of obj.
Inside that singleton class, the methods you define are available only
to obj. They’re not private methods (which would also be available to
other objects of the same class as obj). They’re singleton methods,
which exist only to be called by that one specific object.
When obj is a class, as in class << SomeClass, the singleton methods
pertain to the class object, and are also called “class methods”.
(They are also special-cased in one or two ways; but for the most
part, giving singleton methods to class objects is just one variant of
the general practice of giving singleton methods to objects.)
See: http://www.wobblini.net/singletons.html for more explanation and
Upcoming training by David A. Black/Ruby Power and Light, LLC:
* Intro to Rails, London, UK, December 3-6 (by Skills Matter)
See http://www.rubypal.com for details and 2008 announcements!