Float allocator

I have been testing ruby 1.8 on my windows box before I dare adopt it on
my linux box. I came across this odd difference:

In ruby 1.6 I can dup a Float:

$ ruby -v
ruby 1.6.8 (2002-12-24) [i586-linux-gnu]
$ irb
irb(main):001:0> a = 3.14
=> 3.14
irb(main):002:0> a.dup
=> 3.14

In ruby 1.8 I cannot:

$ ruby -v
ruby 1.8.1 (2003-12-25) [i386-cygwin]
$ irb
irb(main):001:0> a = 3.14
=> 3.14
irb(main):002:0> a.dup
NoMethodError: allocator undefined for Float
from (irb):2:in `dup’
from (irb):2

I did a bit of testing and found out that neither Fixnum, Float or
Bignum can be dup’ed. Is this intentional, and if so then how do I go
about copying the descendents of Numeric?

···


dc -e
4ddod3dddn1-89danrn10-dan3+ann6dan2an13dn1+dn2-dn3+5ddan2/9+an13nap

“Anders Borch” spam@deck.dk schrieb im Newsbeitrag
news:t1Z4c.112164$jf4.6624704@news000.worldonline.dk…

I have been testing ruby 1.8 on my windows box before I dare adopt it on
my linux box. I came across this odd difference:

In ruby 1.6 I can dup a Float:

$ ruby -v
ruby 1.6.8 (2002-12-24) [i586-linux-gnu]
$ irb
irb(main):001:0> a = 3.14
=> 3.14
irb(main):002:0> a.dup
=> 3.14

In ruby 1.8 I cannot:

$ ruby -v
ruby 1.8.1 (2003-12-25) [i386-cygwin]
$ irb
irb(main):001:0> a = 3.14
=> 3.14
irb(main):002:0> a.dup
NoMethodError: allocator undefined for Float
from (irb):2:in `dup’
from (irb):2

I did a bit of testing and found out that neither Fixnum, Float or
Bignum can be dup’ed. Is this intentional, and if so then how do I go
about copying the descendents of Numeric?

You don’t. You gain nothing by dup’ing Integer, Float etc. because these
are immutable anyway. You don’t gain anything by copying these.

OTOH, for a uniform invocation these instance’s #dup methods could be
defined to return self.

Regards

robert

Robert Klemme wrote:

I did a bit of testing and found out that neither Fixnum, Float or
Bignum can be dup’ed. Is this intentional, and if so then how do I go
about copying the descendents of Numeric?

You don’t. You gain nothing by dup’ing Integer, Float etc. because these
are immutable anyway. You don’t gain anything by copying these.

My code is dup’ing objects without knowing what type they are, the great
thing would be to be able to use the same code on my numerics as the
code I can use on all other objects.

OTOH, for a uniform invocation these instance’s #dup methods could be
defined to return self.

This should let me use my code unchanged from ruby 1.6 to 1.8. Thanks :slight_smile:

···

Regards

robert


dc -e
4ddod3dddn1-89danrn10-dan3+ann6dan2an13dn1+dn2-dn3+5ddan2/9+an13nap