# BigDecimal singletons

**URL:** <https://www.ruby-forum.com/t/bigdecimal-singletons/168148>\
**Category:** Ruby\
**Created:** [May 16, 2009, 9:32pm UTC](https://www.ruby-forum.com/t/bigdecimal-singletons/168148 "2009-05-16T21:32:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [May 16, 2009, 9:32pm UTC](https://www.ruby-forum.com/t/bigdecimal-singletons/168148/1 "2009-05-16T21:32:25Z")

</div>

Hi,

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 = BigDecimal.new(‘10’)

class \<\< d  
def value  
32  
end  
end

p.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…

d.singleton\_methods.sort

…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  
accessible.

If I replace d = BigDecimal.new(‘10’) with d = String.new(‘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  
missing?

Thanks in advance…

Dave

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [May 16, 2009, 10:52pm UTC](https://www.ruby-forum.com/t/bigdecimal-singletons/168148/2 "2009-05-16T22:52:49Z")

</div>

On Sat, May 16, 2009 at 3:32 PM, Dave B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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  
singleton\_method\_added(symbol)

```
 From Ruby 1.8

```

* * *

```
 Invoked as a callback whenever a singleton method is added to the
 receiver.

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

 _produces:_

    Adding singleton_method_added
    Adding one
    Adding three

```

$ ri -T Numeric#singleton\_method\_added  
----------------------------------------- Numeric#singleton\_method\_added  
singleton\_method\_added(p1)

```
 From Ruby 1.8

```

* * *

```
 Trap attempts to add methods to +Numeric+ objects. Always raises a
 +TypeError+

```

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

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
