为什么 String::sub!() 会改变 Ruby 中克隆对象的原始对象?

发布于 2024-10-03 03:42:09 字数 625 浏览 5 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叹倦 2024-10-10 03:42:09

newFreqfreq 的浅拷贝。这意味着 newFreq 中存储的每个引用都指向存储在 freq 中的对象。您可以独立更改引用指向的位置 (newFreq.name = newFreq.name.sub 'f','newF'),但如果您调用改变对象的方法,则 newFreqfreq 将受到影响。

另请参阅http://en.wikipedia.org/wiki/Object_copy

newFreq is a shallow copy of freq. That means that each of the references stored inside of newFreq points to the object as the ones stored in freq. 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, both newFreq and freq will be affected.

See also http://en.wikipedia.org/wiki/Object_copy

絕版丫頭 2024-10-10 03:42:09

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文