Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > 'Class.inherited' v. 'inherited' syntax inside Class

Reply
Thread Tools

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

 
 
7stud --
Guest
Posts: n/a
 
      11-08-2007
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-forum.com/.

 
Reply With Quote
 
 
 
 
Jason Roelofs
Guest
Posts: n/a
 
      11-08-2007
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 -- <(E-Mail Removed)> 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-forum.com/.
>
>

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

 
Reply With Quote
 
 
 
 
mortee
Guest
Posts: n/a
 
      11-08-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.


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


 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      11-08-2007
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.rubypal.com for details!

 
Reply With Quote
 
Phrogz
Guest
Posts: n/a
 
      11-08-2007
On Nov 8, 4:32 pm, "David A. Black" <(E-Mail Removed)> 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.

 
Reply With Quote
 
Gary Wright
Guest
Posts: n/a
 
      11-08-2007

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




 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      11-09-2007
Hi --

On Fri, 9 Nov 2007, Phrogz wrote:

> On Nov 8, 4:32 pm, "David A. Black" <(E-Mail Removed)> 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.rubypal.com for details!

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      11-09-2007
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-forum.com/.

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      11-09-2007
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-forum.com/.

 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      11-09-2007
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.rubypal.com for details!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Decralation of class inside other class and definition outside this class =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki C++ 2 07-12-2007 11:52 PM
Templated function inside a template class. Syntax. Thormod Johansen C++ 3 04-19-2007 04:58 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Dynamic temp. datagrid col.gen. -Session access inside a class inside a UserCtrl Andy Eshtry ASP .Net 0 03-01-2004 11:48 PM



Advertisments