Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6561771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:37:43+00:00 2026-05-25T13:37:43+00:00

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

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:37:44+00:00Added an answer on May 25, 2026 at 1:37 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried to use object expression to extend the IDelegateEvent, but in fsi there
Tried this: $('.link').click(function(e) { $.getScript('http://www.google.com/uds/api?file=uds.js&amp;v=1.0', function() { $('body').append('<p>GOOGLE API (UDS) is loaded</p>'); }); return
Does anyone have any insights regarding compiling Ruby code for Windows? I've tried both
Getting a weird error after I've tried to extend the MVC Membership provider in
We have an existing Traceability Matrix in Excel that has columns like: Project Business
In the following code i tried to extend my test plugin. I want to
I wish to extend a base class in C# with some additional functionality. I
Tried to map it from Preferences -> Settings -> Keyboard, but the key combo
Tried something like this: HttpApplication app = s as HttpApplication; //s is sender of
Tried following the instructions here: How to use Google app engine with my own

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.