Ruby 中的数组哈希

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19057960/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 06:12:27  来源:igfitidea点击:

Hash of Arrays in Ruby

rubyarrayshash

提问by user2823083

I have one hash, where key is a string and value is an array of a string. I want something like this:

我有一个哈希,其中键是一个字符串,值是一个字符串数组。我想要这样的东西:

{"k1"=>["v1", "v2"], "k2"=>["v3", "v4"]} 

I have only one hash and one array to implement this. I have coded something like this:

我只有一个散列和一个数组来实现这一点。我已经编码了这样的东西:

hash1 = Hash.new
arr = Array.new
arr.push "v1"
arr.push "v2"
hash1["k1"] = arr 

#hash is like this: {"k1"=>["v1", "v2"]
#Now I clear the array to read the new values

arr. clear
arr.push "v3"
arr.push "v4"
hash1["k2"] = arr
puts hash1

#Output: {"k1"=>["v3", "v4"], "k2"=>["v3", "v4"]}
#Notice that k1's value also got updated

Then I changed one line:

然后我改变了一行:

hash1 = Hash.new
arr = Array.new
arr.push "v1"
arr.push "v2"
hash1["k1"] = arr 

arr = [] # ** This is the only changed line.  Now k1's value is correct. **
arr.push "v3"
arr.push "v4"
hash1["k2"] = arr
puts hash1
#Output: {"k1"=>["v1", "v2"], "k2"=>["v3", "v4"]} (which I wanted)

Can someone please explain me how does this happen? I am very new to Ruby. Ideally, what is the correct way to code this problem?

有人可以解释一下这是怎么发生的吗?我对 Ruby 很陌生。理想情况下,编码此问题的正确方法是什么?

回答by Cary Swoveland

This should show you what is happening (object_idis your friend). (I've inserted an underscore in the Object_id to make it easier to see differences.)

这应该告诉你发生了什么(object_id是你的朋友)。(我在 Object_id 中插入了一个下划线,以便于查看差异。)

hash1 = {}            # => {} 
arr = ["v1", "v2"]    # => ["v1", "v2"] 
arr.object_id         # => 7016637_4343580 
hash1["k1"] = arr     # => ["v1", "v2"] 
hash1                 # => {"k1"=>["v1", "v2"]}
hash1["k1"].object_id # => 7016637_4343580 
arr.clear             # => [] 
arr.object_id         # => 7016637_4343580 
arr << "v3" << "v4"   # => ["v3", "v4"] 
arr.object_id         # => 7016637_4343580 
hash1["k2"] = arr     # => ["v3", "v4"] 
hash1                 # => {"k1"=>["v3", "v4"], "k2"=>["v3", "v4"]} 
hash1["k1"].object_id # => 7016637_4343580 
hash1["k2"].object_id # => 7016637_4166580 

arr = []              # => [] 
arr.object_id         # => 7016637_4036500 
arr = ["v5", "v6"]    # => ["v5", "v6"] 
arr.object_id         # => 7016637_3989880 
hash1                 # => {"k1"=>["v3", "v4"], "k2"=>["v3", "v4"]} 
hash1["k1"].object_id # => 7016637_4343580 
hash1["k2"] = arr     # => ["v5", "v6"] 
hash1                 # => {"k1"=>["v3", "v4"], "k2"=>["v5", "v6"]} 
hash1["k1"].object_id # => 7016637_4343580 
hash1["k2"].object_id # => 7016637_3989880 

回答by konsolebox

The array you saved on the hash is still referenced with arrso clearly doing arr.clearand using arr.pushwould clean up and add new values to the one saved on the hash as well. However doing arr = [], arrwould now reference a new array which is different from the one saved in hash.

你保存在散列上的数组仍然被引用,arr如此清楚地做arr.clear和使用arr.push会清理并向保存在散列上的数组添加新值。但是arr = []arr现在将引用一个新数组,该数组与保存在哈希中的数组不同。

And you can simply add a new array to hash with:

你可以简单地添加一个新数组来散列:

hash1["k2"] = ["v3", "v4"]

Or

或者

hash1["k2"] = %w[v3 v4]