Search code examples
ruby-on-railsrubybigdecimalmonkeypatchingrails-console

Disadantages of defining inspect on BigDecimal


I am using the decimal type in Rails for storing currency.

However, I am tired of constantly seeing results like:

nominal_amount: #<BigDecimal:7f919884b648,'0.7E6',9(18)>

When I use the Rails console.

I can fix this by defining inspect as def inspect; to_s; endon BigDecimal, but I am concerned that it could lead to peculiar bugs.

Anyone who can vouch for or warn against this monkey patch?


Solution

  • Ruby Doc suggests to override inspect for user defined classes. It returns a human readable string. No other code should rely on the functionality of inspect. So if you decide, that it is more human-readable - to you - if you change the default behavior it is okay. As long as you don't include it in a gem or other code foreign developers will use.

    http://ruby-doc.org/core-2.1.1/Object.html#method-i-inspect