Ruby numbers are immutable.  Even if you could pass in a reference to a
number, you would not be able to modify that number.  In fact, passing
around an immediate FixNum object *has the same semantics* as passing around
a reference to a number (for example, as happens when passing around a
BigNum).  The immediate nature of the FixNum is just an optimisation used by
the Ruby interpreter.

I think what you are asking is how can you implement pass by reference in
Ruby.  Ruby only has pass by value.  Even object references (aka pointers)
are passed by value.

There are a number of approaches for approximating pass by reference.  Some
people have suggested blocks.   Other ways will require passing a reference
to an object into the method and modifying the object within that method.
The easiest way will be to use a single-element array.

----- Original Message -----
From: "Guillaume Cottenceau" <gc / mandrakesoft.com>
To: "ruby-talk ML" <ruby-talk / ruby-lang.org>
Sent: Tuesday, September 25, 2001 10:19 PM
Subject: [ruby-talk:21685] manipulating "immutable" objects such as Fixnum
from within callbacks & al...


> Hello,
>
>
> Here's the problem: it happens often that you need to pass "references" to
> objects so that callbacks/functions/etc may modify these objects to their
> convenience, and you will need the modified value afterwards.
>
> Now with "immutable" objects such as Fixnum (also described as objects
> that have "immediate" values by the pickaxe book --> you can't mutate a
> given instance) I can't directly do that... and since "everything" is
> references in Ruby I don't know how to pass a reference to a Fixnum apart
> doing the following, that is an object which is basically a container
> holding one object (o) :
>
> class ObjContainer
>     attr_accessor :o
>     def initialize(v); @o = v; end
> end
>
> def doit(i);  i += 1;   end
> def doit_(j); j.o += 1; end
>
> # example of failure
> t = 3
> doit(t)
> puts t
> #=> 3
>
> # example of success with ObjContainer
> k = ObjContainer.new(3)
> doit_(k)
> puts k.o
> #=> 4
>
>
> Is there a cleaner way to do that ? I tried to look at the toc of the
> pickaxe, and also grep it out, but I didn't find something...
>
>
> --
> Guillaume Cottenceau - http://people.mandrakesoft.com/~gc/
>