D
Dave Birch
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
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