From: RichardOnRails Date: 2009-11-05T04:05:09+09:00 Subject: Re: Ruby doesn't implement x++ for Fixnum's because ??? On Nov 4, 9:37 am, Seebs wrote: > On 2009-11-04, Marnen Laibow-Koser wrote: > > > I believe you are quite wrong.  If a destructive function like gsub! can > > be implemented as a method, then I see no reason that +=, |=, or postfix > > ++ couldn't be. > > gsub! is implemented as a method on objects which contain data.  ++ would > have to be implemented as a method on objects which ARE their data -- which > have no distinction between the object and its "contents". > > gsub! can work because somewhere inside the object there is a hunk of storage > which is separate from the object itself.  Fixnum has no such storage to > refer to. > > -s > -- > Copyright 2009, all wrongs reversed.  Peter Seebach / usenet-nos...@seebs.nethttp://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated! > ++ would have to be implemented as a method on objects which ARE their data Not true. Check out the following, step by step: def show(v) "Got #{v}, class = #{v.class}, object_id = #{v.object_id} (v.object_id-1)/2 = #{(v.object_id-1)/2 }" end class Fixnum def pp # We can’t define ++ because of a compiler restriction. self + 1 end end These lines show that a & b values are stored in there object_ids held in the symbol table. Don’t believe it? Read more. a = 1; show (a) => Got 1; class = Fixnum; object_id = 3; v >> 1 = 1 b = 1; show (b) => Got 1; class = Fixnum; object_id = 3; v >> 1 = 1 a == b => true Appending these lines shows that a & b values are distinct. That is, after incrementing, a =2, b is unchanged; b is not impacted by a’s change a += 1; show (a) => Got 2; class = Fixnum; object_id = 5; v >> 1 = 2 show (b) => Got 1; class = Fixnum; object_id = 3; v >> 1 = 1 Appending these lines shows the ++’s alias pp works just find a=1; show(a.pp) => Got 2; class = Fixnum; object_id = 5; v >> 1 = 2 show(b) => Got 1; class = Fixnum; object_id = 3; v >> 1 = 1 Appending these lines show that ++ crosses the Fixnum/Bignum boundary a = 2**30-1; show (a) => Got 1073741823; class = Fixnum; object_id = 2147483647; v >> 1 = 1073741823 show(a.pp) => Got 1073741824; class = Bignum; object_id = 22738520; v >> 1 = 11369260 # “v >> 1” is irrelevant, of course. Do you agree? Best wishes, Richard