我承认我有点像红宝石新手(现在正在编写 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