Ruby array manipulation inside method

后端 未结 3 1825
傲寒
傲寒 2021-01-19 01:05

In the following, input_1 changes:

def method_1(a)
  a << \"new value\"
end

input_1 = []
method_1(input_1)
input_1 #=> [\"new value\"]         


        
3条回答
  •  抹茶落季
    2021-01-19 01:32

    Why input_1 changes whereas input_2 doesn't change?

    The very simple answer to this question is that your premise is wrong. input_1 doesn't change. The object that input_1 references changes, but that is something completely different from input_1. The name of a thing is not the same as the thing itself. (Outside of witchcraft.)

提交回复
热议问题