Wow, excellent!

Sab

----- Original Message ----- 
From: "Brian Candler" <B.Candler / pobox.com>
To: "ruby-talk ML" <ruby-talk / ruby-lang.org>; <kgergely / mlabdial.hit.bme.hu>
Cc: "ruby-talk ML" <ruby-talk / ruby-lang.org>
Sent: Saturday, October 12, 2002 1:56 PM
Subject: Re: Things That Newcomers to Ruby Should Know


> > If this is true, a+=1 creates a new object. Can't it be avoided?
>
> [...]
>
> a=1      # a is a reference to the number 1 (object of class Fixnum)
> a+=1     # a is now a reference to the number 2
> 
> However in practise this is efficient, since there appears to be a very
> compact internal representation of references to Fixnums:
> 
> irb(main):001:0> 0.id
> 1
> irb(main):002:0> 1.id
> 3
> irb(main):003:0> 2.id
> 5
> 
> So I don't think that an object representing "the number 2" is actually
> allocated on the heap, thankfully :-)
> 
> Hence "a+=1" does not increment the object pointed to by 'a' (you can't
> increment the number 1, it's meaningless); rather, 'a' is changed to
> reference a new object which is the result of applying method +(1) to the
> original object.