Copy/Inheritance of Ruby Matrix class (core/std lib)

核能气质少年 提交于 2019-12-07 13:43:05

问题


I tried to extend an existing Singleton class in Ruby, as an example the Matrix class.

My first quick&dirty solution was a monkey patch (reopen class and extend with functionality).

But I think, monkey patching in general isn't good, especially if someone tries to overwrite basic methods of core classes like in String, Integer, ...

Next step was to find out, how I can get a real hard copy of the Matrix class with a new name (like MatrixExt) which behaves as a standalone singleton.

MatrixExt = Matrix

didn't the job, as it results in:

MatrixExt.scalar(2,0)
=> Matrix[[0, 0], [0, 0]]

So I only get multiple names for same singleton. Not, what I want.

Same result with the clone and the dup methods.

Also class inheritance won't work:

class MatrixExt < Matrix
  # patches ...
end

MatrixExt.scalar(2,0)
=> Matrix[[0, 0], [0, 0]]

And that was the most confusing part, because in self defined classes it is possible to get an inherited class. (So, why the core/std lib classes work different?)

My current solution is to have a module with extension and then explicitly use .extend after initializing, like:

m = Matrix.scalar(2,0).extend(MatrixExtModule)

That is okay for now, but my question is:

IS there another solution and -when yes- how to do it?

(No, copying the matrix.rb is not a good way, of course. ;o)

What I do wrong or where I think in a wrong way?

Thanks in advance for any solution and/or food for thoughts!


回答1:


This is a bug.

I've created an issue on redmine.ruby-lang.org, which is the recommended thing to do to get these fixed.

I fixed the library, but I'm afraid it won't be available until Ruby 1.9.4.




回答2:


If you look at how Matrix is implemented, you'll notice that all the methods like scalar, diagonal etc. call the private new method, which will always return a new Matrix object (you don't override the methods, so Ruby will go look at the superclass implementation, where the implicit receiver of new is self, viz the Matrix class).

I guess your best bet is to wrap all your patches in a module and monkey patch Matrix that way.



来源:https://stackoverflow.com/questions/6064902/copy-inheritance-of-ruby-matrix-class-core-std-lib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!