From: Jim Freeze Date: 2002-12-14T21:45:54+09:00 Subject: Re: Inheritance Question On Saturday, 14 December 2002 at 19:45:03 +0900, ts wrote: > >>>>> "a" == ahoward writes: > > a> p ((mm + Matrix[[12]]).type) # >> MyMatrix > a> p ((Matrix[[12]] + mm).type) # >> Matrix > a> p ((mm + mm).type ) # >> MyMatrix > > Now you have a big problem, because this not what ruby do > > pigeon% cat b.rb > #!/usr/bin/ruby > class A < Array; end > a = A.new > p ((a+[]).class) > p (([]+a).class) > p ((a+a).class) > pigeon% > > pigeon% b.rb > Array > Array > Array > pigeon% Don't you mean that this is how Array was implemented, not what ruby does. Maybe Array is not doing the right thing by not returning self (I'm sure someone will correct me here if I am wrong). The language doesn't force one to always return the base class. class A def +(o) self end end class B < A def +(o) self end end a=A.new b=B.new p ((a+a).class) #=> A p ((b+b).class) #=> B p ((a+b).class) #=> A p ((b+a).class) #=> B -- Jim Freeze