From: Ian Hobson Date: 2003-12-23T17:51:55+09:00 Subject: Re: Copying methods from one class to another In message <200312161434.02097.transami@runbox.com>, T. Onoma writes >On Tuesday 16 December 2003 10:51 pm, Hacksaw wrote: >> >> No. Why would B and C be equal? B is a subclass of C. Specifically, methods >> and variable defined in B are not reflected in C. >> >> The only way that they are equal is that an inherited method in B should do >> the same thing as in C. Same with inherited variables. > >That's okay, I had the same difficulties understanding this at firs too. Let >back up a bit. Look at this: > > A = "1" > B = "2" > A = B > >What happens? > > p A # => "2" > >It's the same thing for classes. I know it's tempting to "over think" when >classes are being delt with, I did the very same thing before I understood >what was going on. But it really is that simple. After all, classes are >objects just like strings. > >So lets do it again, but this time with less "syntatical sugar": > > A = Class.new > B = Class.new(A) # A is the superclass of B > A = B > >All that's happened is that A no longer points to its former self. Rather it >now points to the same thing B does. The old A still exists as the superclass >of B, but we no longer have a convienent constant to reference it by. Note >that constants in Ruby are rather variable. The only thing that is noticably >different is that Ruby spits out a warning saying we have redefined a >constant. > >So, the short notation of > > class A < A > >is the same as: > > A = Class.new(A) > >and does the exact same thing as above, except that it dispenses with the go >between constant B. And is analogous to doing this with a string: > > str = " A new string " + str > This still does not answer the question of why? (At least for me), so I'm jumping in here are asking it again! When you can simply extend class A with any new method you need, why do you want to create a subclass and pretend it is the class you started with? Regards Ian -- Ian - posting to a Newsgroup. Please remove everything to reply.