Hi --
Hi,
I'm looking at the rails activerecord base.rb file it's main structure
is shown below. Why have a class inside a class like this? What
advantages are there?
Thanks,
Peter
class Base
# methods ...
class << self # Class methods
# methods ...
end
# methods ...
end
Every object in Ruby can have methods of its own, in addition to the
methods it gets from being an instance of its class. Those
"singleton" methods (singleton in the sense of belonging to only one
object, not all instances of a class) live in the object's "singleton
class."
The notation:
class << obj
# ...
end
is how you open up a definition block for the singleton class of
obj. Once you're inside that block, it's just like being inside any
other class definition block -- except that you're defining methods
that only obj will be able to call.
Here's an example:
class C # class definition for all instances of C
def speak
puts "Hi"
end
end
c = C.new
class << c # singleton class of this particular instance of C
def speak
puts "Hello"
end
end
another_c = C.new
another_c.speak # Hi
c.speak # Hello
The example you gave was along the lines of:
class C
class << self
def m
(etc.)
In this case (which is a very common one), the object whose singleton
class you're accessing is the Class object C itself. ("self" is equal
to C at the top level of the class definition body.) So you're
creating methods that will only be callable with C as a receiver:
C.m
Those are class methods. Or, to put it another way, class methods are
singleton methods of Class objects.
You can also define a method for a specific object by doing this:
def c.speak
or this, if it's a class:
def C.speak
or (also for a class):
class C
def self.speak
In other words, in most cases you don't have to manually open up the
singleton class. (But see Mauricio's point about the difference in
constant scope, though in most cases that won't be an issue.)
For more on this, see
http://www.wobblini.net/~dblack/i0nas.txt
David