From: Jim Freeze Date: 2002-12-13T14:08:54+09:00 Subject: Re: Inheritance Question Well duh, as soon as I hit send I realized that I had not looked far enough and that what I had posted would not work. The way Matrix is currently written, it is nearly impossible to extend through inheritance. So, instead of using a 'is-a' type class, below is a 'has-a' type class. This method should work but has the drawback as before, every public method of Matrix that you want exposed must be wrapped. require 'matrix' class M def initialize(a) @m = Matrix[*a] end private :initialize def self.[](*a) M.new(a) end def inv M[*@m.inv.to_a] end def inv! # this is new. ie, not in Matrix @m = @m.inv self end def to_f M[*(@m.to_f.to_a)] end def to_f! # this is new and handy! @m = @m.to_f self end def inspect; @m.inspect end end m = M[[1,2],[3,4]] puts m.inspect puts "m.class: #{m.class}" puts "m.to_f.class: #{m.to_f.class}" puts "m.to_f.inv.class: #{m.to_f.inv.class}" puts "m.inv.class: #{m.inv.class}" puts "m.to_f.inv.inspect: #{m.to_f.inv.inspect}" m = m.to_f puts "m.inv!.class: #{m.inv!.class}" puts m.inspect ---with output Matrix[[1, 2], [3, 4]] m.class: M m.to_f.class: M m.to_f.inv.class: M m.inv.class: M m.to_f.inv.inspect: Matrix[[-2.0, 1.0], [1.5, -0.5]] m.inv!.class: M Matrix[[-2.0, 1.0], [1.5, -0.5]] -- Jim Freeze ---------- Kin, n.: An affliction of the blood