如何在 Ruby 中复制哈希值?
我承认我有点像 ruby 新手(现在正在编写 rake 脚本)。在大多数语言中,复制构造函数很容易找到。找了半个小时没有在ruby中找到。我想创建哈希的副本,以便可以修改它而不影响原始实例。
一些预期的方法无法按预期工作:
h0 = { "John"=>"Adams","Thomas"=>"Jefferson","Johny"=>"Appleseed"}
h1=Hash.new(h0)
h2=h1.to_hash
与此同时,我采用了这种不优雅的解决方法
def copyhash(inputhash)
h = Hash.new
inputhash.each do |pair|
h.store(pair[0], pair[1])
end
return h
end
I'll admit that I'm a bit of a ruby newbie (writing rake scripts, now). In most languages, copy constructors are easy to find. Half an hour of searching didn't find it in ruby. I want to create a copy of the hash so that I can modify it without affecting the original instance.
Some expected methods that don't work as intended:
h0 = { "John"=>"Adams","Thomas"=>"Jefferson","Johny"=>"Appleseed"}
h1=Hash.new(h0)
h2=h1.to_hash
In the meantime, I've resorted to this inelegant workaround
def copyhash(inputhash)
h = Hash.new
inputhash.each do |pair|
h.store(pair[0], pair[1])
end
return h
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
由于 Ruby 有一百万种方法可以做到这一点,因此这是使用 Enumerable 的另一种方法:
Since Ruby has a million ways to do it, here's another way using Enumerable:
您可以使用下面的方法来深度复制 Hash 对象。
you can use below to deep copy Hash objects.
对我有用的 Deep_Copy 的替代方法。
这产生了一个 deep_copy,因为 h2 是使用 h1 的数组表示形式而不是 h1 的引用形成的。
Alternative way to Deep_Copy that worked for me.
This produced a deep_copy since h2 is formed using an array representation of h1 rather than h1's references.
clone
方法是 Ruby 的标准构建方法- 进行浅拷贝的方式:请注意,该行为可能会被覆盖:
The
clone
method is Ruby's standard, built-in way to do a shallow-copy:Note that the behavior may be overridden:
正如其他人指出的那样,
clone
会做到这一点。请注意,哈希的克隆
会生成浅表副本。也就是说:发生的情况是散列的引用被复制,但引用引用的对象没有被复制。
如果您想要深层复制,那么:
deep_copy
适用于任何可以编组的对象。大多数内置数据类型(数组、散列、字符串等)都可以进行编组。编组是Ruby对序列化的名称。通过编组,对象及其引用的对象被转换为一系列字节;然后这些字节被用来创建另一个像原始对象一样的对象。
As others have pointed out,
clone
will do it. Be aware thatclone
of a hash makes a shallow copy. That is to say:What's happening is that the hash's references are being copied, but not the objects that the references refer to.
If you want a deep copy then:
deep_copy
works for any object that can be marshalled. Most built-in data types (Array, Hash, String, &c.) can be marshalled.Marshalling is Ruby's name for serialization. With marshalling, the object--with the objects it refers to--is converted to a series of bytes; those bytes are then used to create another object like the original.
如果您使用 Rails,您可以执行以下操作:
http://apidock.com/rails/Hash/deep_dup
If you're using Rails you can do:
http://apidock.com/rails/Hash/deep_dup
Hash 可以从现有的哈希值创建新的哈希值:
Hash can create a new hash from an existing hash:
正如 Marshal 文档的安全注意事项部分中提到的,
以下是如何在 Ruby 中使用 JSON 进行克隆的示例:
As mentioned in Security Considerations section of Marshal documentation,
Here is an example on how to do cloning using JSON in Ruby:
我也是 Ruby 的新手,在复制哈希时也遇到了类似的问题。使用以下内容。我不知道这种方法的速度。
I am also a newbie to Ruby and I faced similar issues in duplicating a hash. Use the following. I've got no idea about the speed of this method.
使用
Object#clone
:(令人困惑的是,
clone
的文档说initialize_copy
是重写此方法的方法,但Hash
中该方法的链接会引导您replace
相反...)Use
Object#clone
:(Confusingly, the documentation for
clone
says thatinitialize_copy
is the way to override this, but the link for that method inHash
directs you toreplace
instead...)由于标准克隆方法保留了冻结状态,因此如果您希望新对象与原始对象略有不同(如果您喜欢无状态编程),则它不适合基于原始对象创建新的不可变对象。
Since standard cloning method preserves the frozen state, it is not suitable for creating new immutable objects basing on the original object, if you would like the new objects be slightly different than the original (if you like stateless programming).
克隆速度很慢。为了提高性能,可能应该从空白散列和合并开始。不包括嵌套哈希的情况...
Clone is slow. For performance should probably start with blank hash and merge. Doesn't cover case of nested hashes...
这是一个特殊情况,但如果您从想要获取并复制的预定义哈希开始,您可以创建一个返回哈希的方法:
我遇到的特殊情况是我有一个 JSON 集合- 架构哈希,其中一些哈希是建立在其他哈希之上的。我最初将它们定义为类变量并遇到了这个复制问题。
This is a special case, but if you're starting with a predefined hash that you want to grab and make a copy of, you can create a method that returns a hash:
The particular scenario that I had was I had a collection of JSON-schema hashes where some hashes built off others. I was initially defining them as class variables and ran into this copy issue.