为什么 String::sub!() 会改变 Ruby 中克隆对象的原始对象?
我的 Ruby 代码中有一个结构,稍后看起来有点像这样
Parameter = Struct.new(:name, :id, :default_value, :minimum, :maximum)
,我使用创建此结构的实例
freq = Parameter.new('frequency', 15, 1000.0, 20.0, 20000.0)
在某些时候,我需要此结构的精确副本,所以我调用
newFreq = freq.clone
然后,我更改 newFreq > 的名字
newFreq.name.sub!('f', 'newF')
也奇迹般地改变了 freq.name
!
像 newFreq.name = 'newFrequency'
这样的简单赋值不会改变 freq
。
这是应该的工作方式吗?
编辑:使用类而不是结构并重载clone
来进行深层复制是个好主意吗?
I have a struct in my Ruby code that looks somewhat like this
Parameter = Struct.new(:name, :id, :default_value, :minimum, :maximum)
later, I create an instance of this struct using
freq = Parameter.new('frequency', 15, 1000.0, 20.0, 20000.0)
At some point, I need an exact duplicate of this struct, so I call
newFreq = freq.clone
Then, I change newFreq
's name
newFreq.name.sub!('f', 'newF')
Which, miraculously, changes freq.name
, too!
A simple assignment like newFreq.name = 'newFrequency'
does not change freq
.
Is this the way this is supposed to work?
Edit: Is it a good idea to use a class instead of a struct and overload clone
to make a deep copy?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
newFreq
是freq
的浅拷贝。这意味着newFreq
中存储的每个引用都指向存储在freq
中的对象。您可以独立更改引用指向的位置 (newFreq.name = newFreq.name.sub 'f','newF'
),但如果您调用改变对象的方法,则newFreq
和freq
将受到影响。另请参阅http://en.wikipedia.org/wiki/Object_copy
newFreq
is a shallow copy offreq
. That means that each of the references stored inside ofnewFreq
points to the object as the ones stored infreq
. You can change where the references point independantly (newFreq.name = newFreq.name.sub 'f','newF'
), but if you call a method that mutates the object, bothnewFreq
andfreq
will be affected.See also http://en.wikipedia.org/wiki/Object_copy
Object#clone
方法执行浅复制。您需要深复制才能完成工作。请点击此链接了解 Ruby 中的深度复制。
The
Object#clone
method performs shallow copy. You need deep copy to get the job done.Follow this link to learn about deep copy in Ruby.