【发布时间】:2019-01-08 10:53:58
【问题描述】:
在下文中,input_1 发生变化:
def method_1(a)
a << "new value"
end
input_1 = []
method_1(input_1)
input_1 #=> ["new value"]
在下面,input_2 不变:
def method_2(a)
a = ["new value"]
end
input_2 = []
method_2(input_2)
input_2 #=> []
为什么input_1 会改变而input_2 不会改变?
【问题讨论】:
-
您的问题到底是什么?您希望两次输入的值都是
['new value']? -
为什么改变 input_1 而 input_2 没有改变?
-
这个概念被称为“可变性”。
method_2只是分配一个与另一个同名的新变量。
标签: ruby local-variables