Can anyone expand upon, correct, or verify what I feel is happening when you pass arguments to a method in Ruby. Are any of these points wrong? Am I missing any pieces?
- Everything in Ruby is an object.
- Variables are references to objects
- (When passing in a variable into a method): The parameter in the method that catches the variable is a local variable to that method. The parameter (local variable) now also has a reference to the same object.
- I could alter the object (in place) and this alteration will hold when the method scope is exited. Any variables referencing this object outside the method scope will reflect that the object has been altered.
- A new assignment to that parameter (local variable) does not change the original object, thus any references to it when the method leaves scope will remain unchanged.
- If I am passing a variable into the method that references an Integer there is effectively no way that once that method exits I could have that variable referencing a new Integer.
Is there any way to have a method that takes as one of its parameters an Integer, does some stuff, and maybe as a side effect changes the value, having that change reflected once the method exits. Maybe I am just not thinking “the Ruby way”.
Close enough.
No. A variable “names” an object: when a variable is evaluated, it evaluates to the object that it currently “names”. Internally this is done by “storing a pointer” (or equivalent mechanism) to an object. (Although an implementation does not need to always use pointers: in Ruby MRI, for instance, Fixnum values actually exist without a real object.)
No. See above. However, both variables now name (or “evaluate to”) the same object. The parameters are passed internally using Call-by-Value — that is, internally, the pointers to the objects are passed — although Ruby has Call-by-Object-Sharing semantics, which is a term I try to promote as I find it succinctly describes the behavior.
Yes, an object is itself. If you mutate that object, you mutate that object everywhere. But note: none of the variables are changed. Both the inside and the outside variables will still name (or “evaluate to”) the same object.
Correct. If you assign a different value to the local variable you make it, the local variable, name a different object. Ruby is not Call-by-Reference so the variable in the calling context is not altered.
Variables are never passed. Variables are evaluated to the objects they name and those objects are passed. Anyway, we know that:
Thus:
can never change what
xnames, nor can it even change the contents of the objectxnames (because it’s, well, immutable). Even if the object thatxnamed was mutable, the method could not have changed what objectxnames: it could only have mutated the object that resulted from the evaluation ofx.Happy coding.
Now, The Ruby Way — in my book — would be to use a better return value that compassed all the new state, and let the caller put it where it needs to go 🙂
Of course, mutable objects (including simple arrays) are also an option, but that’s ick. And, if there is enough state that travels together, it might be a candidate for a separate class.
As a closing note: Ruby supports a concept of closures, so it is possible in a lexically-scoped manner:
(This was shown for a simple lambda, but it is possible to design/craft a method to work similarly: in all the silly counter-examples like this, the outside variable itself needs to be known, however, as there is no way for the lambda/method of make an outside variable name a new object otherwise.)