Forum: Ruby BigDecimal singletons

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
C31cc64659c8c1bba84afc892eaba992?d=identicon&s=25 Dave Birch (daveb1976)
on 2009-05-16 21:32

I'm a ruby nuby and I'm confused about something.  When I try to create
a singleton method for a BigDecimal like so:

require 'bigdecimal'
d ='10')

class << d
  def value


I get an error:
singtest.rb:7:in `singleton_method_added': can't define singleton method
"value" for BigDecimal (TypeError)

Doing this through IRB, I was surprised that if I then ran...


...after getting the error above, I get "value" listed as a singleton
method, and indeed, d.value yields 32.

If I wrap the singleton method definition in a begin...rescue block, the
TypeError is caught and not displayed, and the singleton method is

If I replace d ='10') with d ='10'), but
leave the rest of the code the same, I don't get the error and the
singleton method works as expected.  Is there something simple I'm

Thanks in advance...

8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2009-05-16 22:52
(Received via mailing list)
On Sat, May 16, 2009 at 3:32 PM, Dave Birch <>
>    32
> leave the rest of the code the same, I don't get the error and the
> singleton method works as expected.  Is there something simple I'm
> missing?

The Numeric class overrides singleton_method_added to raise an error

$ ri -T Object#singleton_method_added
------------------------------------------ Object#singleton_method_added

     From Ruby 1.8
     Invoked as a callback whenever a singleton method is added to the

        module Chatty
          def Chatty.singleton_method_added(id)
            puts "Adding #{id.id2name}"
          def     end
          def two()          end
          def Chatty.three() end


        Adding singleton_method_added
        Adding one
        Adding three

$ ri -T Numeric#singleton_method_added
----------------------------------------- Numeric#singleton_method_added

     From Ruby 1.8
     Trap attempts to add methods to +Numeric+ objects. Always raises a

I guess this is to avoid surprises since certain kinds of numerics
can't have singleton methods (e.g. Fixnums which because of their
implementation don't have a klass pointer so can't acquire a singleton
class).  So it wouldn't make sense to be able to add a singleton
method to 100 factorial (a Bignum), but not to 100 (a Fixnum).

However, the singleton_method_added callback happens after the method
has been added so it's kind of like locking the barn door after the
horse has left.

Rick DeNatale

This topic is locked and can not be replied to.