Is that actually true – that all function calls are by reference? My understanding was that things are passed around by value, but that most (all?) values are references to things.
Right. Functions calls are references passed by value. :-/ So foo(bar) creates a new reference variable in the function pointing to what bar pointed to.
Assignment points the local reference to something else.
This can be tricksy with integers because there are no functions that modify them through the reference, because they aren't actually independent objects that can be referenced under the covers.
Thus it's quite impossible to write stuff like the following C fragment (for integers that is).
foo(int *i) { *i = 1; }
07 Jul, 2009, David Haley wrote in the 24th comment:
Votes: 0
I thought that you could achieve that fairly easily with a wrapper class around the number one way or another, kind of like what a mutable Integer class might look like in Java. It's non-obvious, though, I'll definitely agree. And you wouldn't be able to use actual assignment.
Any immediate object isn't stored by reference. Nor is the reference passed. Integers are immediate objects whereas most (all?) other objects are not.
edit: Fixnum, Symbol, true, false, and nil are all examples of immediate objects. /edit
So for one of these immediate objects to have a reference passed an object with a reference to this immediate object must be passed. Example of a fast way to get the same effect follows:
def roar arr arr[0] = 1 end
i = 0 roar [i] ### i is now 1
Also, keep in mind:
foo(int *i) { *i = 1; }
*i = 1; is indeed the same thing as i[0] = 1;
07 Jul, 2009, David Haley wrote in the 26th comment:
Votes: 0
Is the "immediate object" Ruby's version of a "primitive value"?
I should also say that once a Fixnum has a value outside of its operation it becomes a Bignum instance. Bignum and Fixnum implement the same methods. Which is indeed its own instance—