On 5/31/06, Wes G. firstname.lastname@example.org wrote:
Posted via http://www.ruby-forum.com/.
Here’s an excerpt from Ruby for Rails by Black that does a good job of
Defining class methods with class <<
By far the most frequent use of the class << notation for entering a
class is in connection with class method definitions. You’ll see this
class << self
This code results in a class method Ticket.most_expensive. That method
also be defined like this (assuming this code comes at a point in the
where the Ticket class already exists):
class << Ticket
Because self is Ticket inside the class Ticket definition body, class <<
inside the body is the same as class << Ticket outside the body.
could do class << Ticket even inside the body of class Ticket, but in
you’ll usually see class << self whenever the object whose singleton
opening is self.)
The fact that class << self shows up frequently in connection with the
tion of class methods sometimes leads to the false impression that the
notation can only be used to create class methods, or that the only
can legally put on the right is self. In fact, class << self inside a
block is just one case of the class << object notation. The technique is
puts you in a definition block for the singleton class of object,
may be. That, in turn, means you’re operating in a context where
doâ??whatever you add to the classâ??pertains only to that one object.