[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

dup and clone

Raj Sahae

3/1/2007 9:57:00 PM

Why?

irb(main):012:0> a = 5
=> 5
irb(main):013:0> a.class
=> Fixnum
irb(main):014:0> a.methods.include?("dup")
=> true
irb(main):015:0> a.methods.include?("clone")
=> true
irb(main):016:0> a.dup
TypeError: can't dup Fixnum
from (irb):16:in `dup'
from (irb):16
from :0
irb(main):017:0> a.clone
TypeError: can't clone Fixnum
from (irb):17:in `clone'
from (irb):17
from :0
irb(main):018:0>

4 Answers

Rick DeNatale

3/1/2007 10:16:00 PM

0

On 3/1/07, Raj Sahae <rajsahae@gmail.com> wrote:
> Why?
>
> irb(main):012:0> a = 5
> => 5
> irb(main):013:0> a.class
> => Fixnum
> irb(main):014:0> a.methods.include?("dup")
> => true
> irb(main):015:0> a.methods.include?("clone")
> => true
> irb(main):016:0> a.dup
> TypeError: can't dup Fixnum
> from (irb):16:in `dup'
> from (irb):16
> from :0
> irb(main):017:0> a.clone
> TypeError: can't clone Fixnum
> from (irb):17:in `clone'
> from (irb):17
> from :0
> irb(main):018:0>
>
>

Because Fixnum's , like some other things like Symbols, nil, true,
false are immediate valued objects, there is only one instance of 1
just like there is only one instance of true, nil, or :abc. Since dup
and clone are defined to create a new object like the original, they
can't work on these objects.

Now as to why Fixnum has a dup method. That's just an implementation
choice. It is really getting it from the Kernel module. The
implementation checks the receiver and if it's an immediate object (or
what the implementation code calls a special constant, it throws that
exception.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Rick DeNatale

3/1/2007 10:18:00 PM

0

On 3/1/07, Rick DeNatale <rick.denatale@gmail.com> wrote:

>
> Because Fixnum's , like some other things like Symbols, nil, true,
> false are immediate valued objects, t

Actually, I realized I mispoke slightly as soon as I sent this. These
are all what the implementation calls special constants which means
that there is a unique instance for a given value. Some of them like
SmallIntegers, nil, true, and false are allso immediate value objects
in that the value of a reference to one of these objects encodes the
value directly rather than acting as a pointer to ram which contains
the actual object.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Gary Wright

3/1/2007 10:31:00 PM

0


On Mar 1, 2007, at 4:56 PM, Raj Sahae wrote:
> Why?
> rb(main):016:0> a.dup
> TypeError: can't dup Fixnum
> from (irb):16:in `dup'
> from (irb):16
> from :0

Read this (long) thread.

Gary Wright




Gary Wright

3/1/2007 11:24:00 PM

0


On Mar 1, 2007, at 5:30 PM, Gary Wright wrote:


>
> On Mar 1, 2007, at 4:56 PM, Raj Sahae wrote:
>
>> Why?
>> rb(main):016:0> a.dup
>> TypeError: can't dup Fixnum
>> from (irb):16:in `dup'
>> from (irb):16
>> from :0
>>
>
> Read this (long) thread.
>


Sorry about that. Not sure why the link didn't show up:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...


Gary Wright