From: Rick DeNatale Date: 2007-03-02T07:15:31+09:00 Subject: Re: dup and clone On 3/1/07, Raj Sahae 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.denhaven2.com/