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

  • SEARCH
  • Home
  • 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 8673221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:24:11+00:00 2026-06-12T19:24:11+00:00

The following code works fine: class Float def round self.format.to_f end def format %.2f

  • 0

The following code works fine:

class Float
  def round
   self.format.to_f
  end

  def format
    "%.2f" % self
  end
end

However, it seems bad practice to monkeypatch a class like Float because other people are doing the same thing and it causes problems.

Then I installed simplecov and the problem started: simplecov monkeypatches the same methods.

So I created a module and mixed it in to Float.

module MyModule
  def round
   self.format.to_f
  end

  def format
    "%.2f" % self
  end
end

Which I guess works as well. But the problem is that simplecov seems to be overwriting the mixed-in method above.

So, what is the proper way to extend built-in classes so that they do not conflict with other people’s code?

Ruby 1.9.3

  • 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-06-12T19:24:12+00:00Added an answer on June 12, 2026 at 7:24 pm

    Why not use just argument on the round call?

    13.6657.round(2)    # => 13.67
    

    But if you are sure you need module (to possibly adjust the format for all Floats out there, I’d propose you just define format method as such:

    module MyModule
      def format
        ("%.2f" % self).to_f
      end
    end
    

    And mix this in to Float.
    And later in code you call the format method instead of round:

    13.6657.format     # => 13.67
    

    This way it does not hurt the core functionality (as your initial code dropped the argument from the round definition).

    Even better – if you want (can) pinpoint the monkey-patching, simply extend specific instance:

    a = 13.6657
    a.extend MyModule
    
    a.format           # => 13.67
    

    This way it wont mess with other Floats, but you can still adjust the format without finding all calls to a.round(2) in your code.

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

Sidebar

Related Questions

The following code works fine. class A { private: int _value; public: class AProxy
The following code works fine for Visual C++ 2008. However, when comes to Visual
The following code works fine, a simple template class with a definition and a
The following code works fine (it builds a generic OrderBy): public class Item {
The following code works fine #include <functional> using namespace std; using namespace std::placeholders; class
// following code works fine n open notepad... class demo { public static void
I have the following code: class MyClass def method foo = MyClass.all end end
Please advise.. In the following code(Tested. Will work fine) public class Exp_Test { public
The following code works fine on Linux but throws an exception on OS X
The following code works fine in firefox but as with many other things, I

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.