wrote:
> I've seen references to singleton methods and class methods. They seem
> to mean the same thing. True?
Not entirely the same: a class method is a singleton method but a singleton
method is not always a class method in the traditional sense
A singleton method is just a method defined on a single object rather than
an entire class of objects. Because it so happens that classes are objects
too (class Class), methods can be defined on a Class object and since those
methods are invoked on the Class object itself they look like 'class methods'.
The syntax 'def self.class_method()' is in fact the exact same as the singleton
method syntax:
class Foo
end
f = Foo.new
def f.foo()
puts 'foo!'
end
f.foo
With class methods the receiving object is 'self', the class itself, rather than
some more conventional object like 'f' in the above example.
It might be useful to think of class instance methods (or class methods for
short) using this mental model:
f = Foo.new; f.instance_method
Versus
Foo = Class.new; Foo.class_method
> Thanks,
> Peter
E