Steven Jenkins
7/7/2005 6:00:00 PM
none wrote:
> Does anyone know why it would be impossible for me to subclass
> GSL::Matrix? This might be evidence of my lack of sophistication in
> Ruby or OO, but perhaps there is an easy or interesting answer. Here
> is the code:
>
> class SuperMatrix < GSL::Matrix
> def initialize
> @foo = 2
> super
> end
> def whatever
> return @foo
> end
> end
You know why now, but this might help:
require 'delegate'
require 'gsl'
class SuperMatrix < DelegateClass(GSL::Matrix)
def initialize(*m)
@foo = 2
sm = GSL::Matrix.new(*m)
super(sm)
end
def whatever
return @foo
end
end
$ irb -r t
irb(main):001:0> m = SuperMatrix.new([1,2,3], [4,5,6])
[ 1.000e+00 2.000e+00 3.000e+00
4.000e+00 5.000e+00 6.000e+00 ]
=> #<GSL::Matrix:0xb796bc14>
irb(main):002:0> m.whatever
=> 2
The fact that 'inspect' is delegated has a somewhat surprising result,
but it appears to do what you want. 'Forwardable' is also related.
Steve