See below, Object_id
will answer your all questions:
class A
def meth1(a)
p a.object_id #=> 11
a = a+5 # you passed a reference to the object `5`,but by `+` you created a new object `10`.
p a.object_id #=> 21
end
def meth2(array)
p array.object_id #=> 6919920
array.pop
p array.object_id #=> 6919920
end
end
obj1 = A.new
aa=5
obj1.meth1(aa)
p aa.object_id #=> 11
arr = [3,4,5]
obj1.meth2(arr)
p arr.object_id #=> 6919920
So its true that in your code object reference is passed, by value
. Note +
create a new object thus reference is to 10
made locally to the method where it got changed.