Coming from a C background, I had similar difficulties; worrying about
pointer versus copy parameter semantics. The answer to my concerns
turned out to be... Why should I have to care?
With Ruby, you don't. It takes a different tact altogether.
This is how I reconciled it.
Try this in IRB:
Set up your outer variables
h = {"a"=>1,"b"=>2,"c"=>3,"d"=>4}
a = [1,2,3,4]
n = 7
But instead of creating your method, just do this:
a_hash, an_arr, a_num = h , a , n # thru parallel assignment
Now, if you execute some of those statements you had in your method,
you should still get the same outcomes, but hopefully not as
surprising in this context.
A quick digression regarding assignment semantics:
The 'local' * Variables *( on the left hand side ) are getting their
initial values
By evaluating the * Expresssions * on the right hand side of the
assignment, which evaluate to a list of Objects. i.e. h, a and n
offered up their objects to initialize the variables on the left.
When you invoke a method, Ruby essentailly performs this kind of
parallel assignment:
- The Arguments in the Invocation are treated as a Right Hand Side
list of expressions that evaluate to Objects.
- The Parameters specified in the method Definition act as the
(local) variables on the Left Hand Side.
To me, that's one of the great incites in Ruby. It lets us write
incredibly flexible methods, courtesy of the parallel assignment
"splatters".