In the following, input_1
changes:
def method_1(a)
a << \"new value\"
end
input_1 = []
method_1(input_1)
input_1 #=> [\"new value\"]
It boils down to Ruby using "pass-reference-by-value".
The exact case that you encounter is described in this excellent blog post.
In method_1
you are changing the value of an object that two different variables (input_1
and a
) are both pointing to.
In method_2
you are reassigning a completely new object to one of the two variables (a
).