[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

'Class.inherited' v. 'inherited' syntax inside Class

7stud --

11/8/2007 8:28:00 PM

How come when you redefine the inherited method in Class, you don't use
the 'class method' syntax? This doesn't work:

class Class
def Class.inherited(class_obj)
puts class_obj
puts
end
end


Instead, you have to write:

class Class
def inherited(class_obj)
puts class_obj
puts

end
end

Yet, in pickaxe2 on p. 445, the book lists inherited as a class method
of Class. And, if I have a class like this:


class Dog
def Dog.speak
puts 'Woof'
end
end

Dog.speak
-->Woof


and I want to redefine Dog.speak, I have to do this:

class Dog
def Dog.speak
puts 'yap yap'
end
end

Dog.speak
-->yap yap
--
Posted via http://www.ruby-....

11 Answers

Jason Roelofs

11/8/2007 8:52:00 PM

0

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

On Nov 8, 2007 3:27 PM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:

> How come when you redefine the inherited method in Class, you don't use
> the 'class method' syntax? This doesn't work:
>
> class Class
> def Class.inherited(class_obj)
> puts class_obj
> puts
> end
> end
>
>
> Instead, you have to write:
>
> class Class
> def inherited(class_obj)
> puts class_obj
> puts
>
> end
> end
>
> Yet, in pickaxe2 on p. 445, the book lists inherited as a class method
> of Class. And, if I have a class like this:
>
>
> class Dog
> def Dog.speak
> puts 'Woof'
> end
> end
>
> Dog.speak
> -->Woof
>
>
> and I want to redefine Dog.speak, I have to do this:
>
> class Dog
> def Dog.speak
> puts 'yap yap'
> end
> end
>
> Dog.speak
> -->yap yap
> --
> Posted via http://www.ruby-....
>
>
I would guess it's because inheritance and everything related to it is
defined at object instantiation, not at Class definition. I would believe
that the Ruby VM doesn't care about class heirarchy until an object is
built, then it needs to look around to find out what's available to this new
object and what's not. You can do

class Class
def Class.some_method
end
end

just fine, but this isn't the problem you're seeing. You're seeing a problem
as to when a certain method is called.

Though please someone correct me if what I've said is wrong, this is just
educated guessing.

Jason

mortee

11/8/2007 9:07:00 PM

0

7stud -- wrote:
> How come when you redefine the inherited method in Class, you don't use
> the 'class method' syntax? This doesn't work:
>
> class Class
> def Class.inherited(class_obj)
> puts class_obj
> puts
> end
> end
>
>
> Instead, you have to write:
>
> class Class
> def inherited(class_obj)
> puts class_obj
> puts
>
> end
> end
>
> Yet, in pickaxe2 on p. 445, the book lists inherited as a class method
> of Class.

I'm not sure what pickaxe writes about this, but how would it be called
on the class that's been inherited if it were a class method of Class?
Then you couldn't redefine it on a per-class basis.

mortee


David A. Black

11/8/2007 11:33:00 PM

0

Hi --

On Fri, 9 Nov 2007, 7stud -- wrote:

> How come when you redefine the inherited method in Class, you don't use
> the 'class method' syntax? This doesn't work:
>
> class Class
> def Class.inherited(class_obj)
> puts class_obj
> puts
> end
> end
>
>
> Instead, you have to write:
>
> class Class
> def inherited(class_obj)
> puts class_obj
> puts
>
> end
> end
>
> 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. 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 a_singleton_method
puts "I'm a singleton method of C"
end
end

C.an_instance_method
C.a_singleton_method

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.


David

--
Upcoming training by David A. Black/Ruby Power and Light, LLC:
* Advancing With Rails, Edison, NJ, November 6-9
* Advancing With Rails, Berlin, Germany, November 19-22
* Intro to Rails, London, UK, December 3-6 (by Skills Matter)
See http://www.r... for details!

Phrogz

11/8/2007 11:47:00 PM

0

On Nov 8, 4:32 pm, "David A. Black" <dbl...@rubypal.com> wrote:
> class Class
> def an_instance_method
> puts "I'm an instance method of Class"
> end
> end
>
> class C
> def a_singleton_method
> puts "I'm a singleton method of C"
> end
> end
>
> C.an_instance_method
> C.a_singleton_method

O.o

David, I suggest that your example above is missing one of:
"self."
"C."
"class << self...end" wrapper

Right? Tell me I'm not insane.

Gary Wright

11/8/2007 11:48:00 PM

0


On Nov 8, 2007, at 6:32 PM, David A. Black wrote:
> class C
> def a_singleton_method
> puts "I'm a singleton method of C"
> end
> end

I think David meant:

> class C
> def self.a_singleton_method # <--- note self
> puts "I'm a singleton method of C"
> end
> end


Gary Wright




David A. Black

11/9/2007 1:36:00 AM

0

Hi --

On Fri, 9 Nov 2007, Phrogz wrote:

> On Nov 8, 4:32 pm, "David A. Black" <dbl...@rubypal.com> wrote:
>> class Class
>> def an_instance_method
>> puts "I'm an instance method of Class"
>> end
>> end
>>
>> class C
>> def a_singleton_method
>> puts "I'm a singleton method of C"
>> end
>> end
>>
>> C.an_instance_method
>> C.a_singleton_method
>
> O.o
>
> David, I suggest that your example above is missing one of:
> "self."
> "C."
> "class << self...end" wrapper
>
> Right? Tell me I'm not insane.

I can't vouch for your sanity :-) But you (and Gary) are totally
(W)right about my error. Thanks for pointing it out; it should indeed
be def C.a_singleton_method (at least, that's the form I meant to use,
to mirror the def Class.inherited example).


David

--
Upcoming training by David A. Black/Ruby Power and Light, LLC:
* Advancing With Rails, Edison, NJ, November 6-9
* Advancing With Rails, Berlin, Germany, November 19-22
* Intro to Rails, London, UK, December 3-6 (by Skills Matter)
See http://www.r... for details!

7stud --

11/9/2007 5:55:00 AM

0

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.
--
Posted via http://www.ruby-....

7stud --

11/9/2007 6:02:00 AM

0

7stud -- wrote:
> 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.
>

Whoops. 'inherit' --> 'inherited'
--
Posted via http://www.ruby-....

David A. Black

11/9/2007 11:23:00 AM

0

Hi --

On Fri, 9 Nov 2007, 7stud -- wrote:

> 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'.

No, I don't think that distinction is useful. You can certainly make
the distinction between instance methods of Class and singleton
methods of Class objects, but in practice, they're both frequently
referred to as "class methods".

> 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.

I think the terminology gets a bit circular at the top of the tree.
inherited is (at least in 1.8.6) defined as a private instance method
of Class. My experience is that instance methods of Class or Module
are often referred to as "class methods". For example, it's common to
refer to attr_accessor as a class method. I don't know whether that
was Dave's thinking in listing it as a class method. It's also a
slightly edge case since it doesn't really do anything unless you
override it.


David

--
Upcoming training by David A. Black/Ruby Power and Light, LLC:
* Advancing With Rails, Edison, NJ, November 6-9
* Advancing With Rails, Berlin, Germany, November 19-22
* Intro to Rails, London, UK, December 3-6 (by Skills Matter)
See http://www.r... for details!

Yossef Mendelssohn

11/9/2007 2:32:00 PM

0

On Nov 8, 5:32 pm, "David A. Black" <dbl...@rubypal.com> wrote:

> 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.
>
> David

I did not know that. You learn something every day.

Of course, I've never considered doing so, which is obviously a good
part of why I didn't know.

--
-yossef