Thanks for the responses.
David A. Black wrote:
Hi –
On Fri, 9 Nov 2007, 7stud – wrote:
Yet, in pickaxe2 on p. 445, the book lists inherited as a class method
of Class.
The term “class method” can refer to either of two things: an instance
method of Class, or a singleton method of a given class.
Wouldn’t it be more proper to call an instance method of Class a “Class
method” rather than a ‘class method’. In any case, I don’t think your
explanation applies to the page I referenced in pickaxe2. Every class
in the reference section starts with the class name a the beginning of a
section, and then the methods for the class are listed on the subsequent
pages. But the methods are divided into the categories: Class methods,
Instance methods, and Private methods. On p. 445, it says:
class Class < Module
…
…
Class methods:
inherited
…
new
…
Instance methods:
allocate
…
new
…
superclass
…
Based on your explanation, I think that is a clear case for the errata.
In fact, I checked ri, and ri produces this:
Class methods:
new
Instance methods:
allocate, inherited, initialize_copy, new, superclass
(END)
When you call
them, the technique is the same:
class Class
def an_instance_method
puts “I’m an instance method of Class”
end
end
class C
def C.a_singleton_method
puts “I’m a singleton method of C”
end
end
C.an_instance_method
C.a_singleton_method
Ok, thanks. That explains why the inherit method in Class, when
overridden in a user defined class, must be defined using the ‘class
method’ syntax, e.g. UserClass.inherit. I thought the method in Class
was named Class.inherit (because pickaxe2 said so), and in order to
override that in a user defined class, you defined it with the same
syntax, e.g. MyClass.inherit.
So… when you do this:
class Class
def inherited …
you’re defining an instance method called “inherited”, and all
instances of Class – that is, all classes – will have that method.
When you define Class.inherited, you’re defining a singleton method on
Class. The only thing that would be affected by that would be if you
created a subclass of Class – but Ruby doesn’t permit that.
Ok. Thanks for clearing things up.