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!