如何在 Ruby 中创建对象的深层副本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8206523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a deep copy of an object in Ruby?
提问by B Seven
I did some searching found some different methods and posts about creating a deep copy operator.
我做了一些搜索,发现了一些关于创建深复制运算符的不同方法和帖子。
Is there a quick and easy (built-in) way to deep copy objects in Ruby? The fields are not arrays or hashes.
是否有一种快速简便的(内置)方法可以在 Ruby 中深度复制对象?这些字段不是数组或散列。
Working in Ruby 1.9.2.
在 Ruby 1.9.2 中工作。
回答by Alex Peattie
Deep copy isn't built into vanilla Ruby, but you can hack it by marshalling and unmarshalling the object:
深拷贝没有内置在普通 Ruby 中,但您可以通过编组和解组对象来破解它:
Marshal.load(Marshal.dump(@object))
This isn't perfect though, and won't work for all objects. A more robust method:
但这并不完美,并且不适用于所有对象。一个更健壮的方法:
class Object
def deep_clone
return @deep_cloning_obj if @deep_cloning
@deep_cloning_obj = clone
@deep_cloning_obj.instance_variables.each do |var|
val = @deep_cloning_obj.instance_variable_get(var)
begin
@deep_cloning = true
val = val.deep_clone
rescue TypeError
next
ensure
@deep_cloning = false
end
@deep_cloning_obj.instance_variable_set(var, val)
end
deep_cloning_obj = @deep_cloning_obj
@deep_cloning_obj = nil
deep_cloning_obj
end
end
Source:
来源:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-list/43424
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-list/43424
回答by user2256822
I've created a native implementation to perform deep clones of ruby objects.
我创建了一个本地实现来执行 ruby 对象的深度克隆。
It's approximately 6 to 7 times faster than the Marshal approach.
它大约比 Marshal 方法快 6 到 7 倍。
https://github.com/balmma/ruby-deepclone
https://github.com/balmma/ruby-deepclone
Note that this project is not maintained anymore (last commit in 2017, there are reported issues)
回答by Mat
There is a native implementation to perform deep clones of ruby objects: ruby_deep_clone
有一个本地实现来执行 ruby 对象的深度克隆:ruby_deep_clone
Install it with gem:
用 gem 安装它:
gem install ruby_deep_clone
Example usage:
用法示例:
require "deep_clone"
object = SomeComplexClass.new()
cloned_object = DeepClone.clone(object)
It's approximately 6 to 7 times faster than the Marshal approach and event works with frozen objects.
它比 Marshal 方法和事件处理冻结对象快大约 6 到 7 倍。
Note that this project is not maintained anymore (last commit in 2017, there are reported issues)
回答by Claudio Floreani
Rails has a recursive method named deep_dupthat will return a deep copy of an object and, on the contrary of dupand clone, works even on composite objects (array/hash of arrays/hashes).
It's as easy as:
导轨具有名为递归方法deep_dup,将返回的对象的深拷贝,并且,在相反的dup和clone,作品即使在复合对象(列/散列的阵列/散列)。这很简单:
def deep_dup
map { |it| it.deep_dup }
end
回答by Adam Luzsi
You can use duplicate gem for this.
您可以为此使用重复的 gem。
It's a small pure ruby gem that able to recursively duplicate object It will duplicate it's object references too to the new duplication.
它是一个小的纯 ruby gem,能够递归复制对象它也会将它的对象引用复制到新的副本。
require 'duplicate'
duplicate('target object')
https://rubygems.org/gems/duplicate
https://rubygems.org/gems/duplicate
回答by lulalala
Automatic deep clone is not always what you want. Often you need to define a selected few attributes to deep clone. A flexible way to do this is to implement the initialize_copy, initialize_dupand initialize_clonemethods.
自动深度克隆并不总是您想要的。通常你需要定义一些选定的属性来进行深度克隆。一种灵活的方法是实现initialize_copy,initialize_dup和initialize_clone方法。
If you have a class:
如果你有一个类:
class Foo
attr_accessor :a, :b
end
and you only want to only deep clone :b, you override the initialize_*method:
并且您只想深度克隆:b,您可以覆盖该initialize_*方法:
class Foo
attr_accessor :a, :b
def initialize_dup(source)
@b = @b.dup
super
end
end
Of course if you want @bto also deep clone some of its own attributes, you do the same in b's class.
当然,如果你还想@b深度克隆它自己的一些属性,你可以在 b 的类中做同样的事情。
Rails does this (see https://github.com/rails/rails/blob/0951306ca5edbaec10edf3440d5ba11062a4f2e5/activemodel/lib/active_model/errors.rb#L78)
For more complete explanation, I learned it here from this post https://aaronlasseigne.com/2014/07/20/know-ruby-clone-and-dup/
为了获得更完整的解释,我从这篇文章中学到了它https://aaronlasseigne.com/2014/07/20/know-ruby-clone-and-dup/
回答by Matthew O'Riordan
You really don't need a Gem for this. This couldn't be much simpler than this, which is not worth the overhead of a Gem!
你真的不需要宝石。这不可能比这简单得多,这不值得 Gem 的开销!
def deep_clone(obj)
obj.clone.tap do |new_obj|
new_obj.each do |key, val|
new_obj[key] = deep_clone(val) if val.is_a?(Hash)
end
end
end
回答by Noman Ur Rehman
I would suggest you use the ActiveSupport gem which adds a lot of sugar to your native Ruby core, not just a deep clonemethod.
我建议您使用 ActiveSupport gem,它为您的原生 Ruby 核心添加了很多糖,而不仅仅是深度克隆方法。
You can look into the documentationfor more information regarding the methods that have been added.
您可以查看文档以获取有关已添加方法的更多信息。
回答by flajann
Also check out deep_dive. This allows you to do controlled deep copies of your object graphs.
另请查看 deep_dive。这允许您对对象图进行受控的深拷贝。

