也许我今天刚刚盯着我的屏幕太久了,但我认为应该是非常基本的东西让我很难受.
我正在尝试对变量进行“复制”,这样我就可以在不修改原始变量的情况下对其进行操作.
# original var is set foo = ["a","b","c"] # i want a copy of the original var so i dont modify the original bar = foo # modify the copied var bar.delete("b") # output the values puts bar # outputs: ["a","c"] - this is right puts foo # outputs: ["a","c"] - why is this also getting modified?
我希望不要改变.
解决方法
foo和bar指的是同一个对象.要使bar引用不同的对象,您必须克隆foo:
bar = foo.clone