2015-10-27 64 views
1

循環在具有特定鍵值對的文本文件上運行三次。每個循環都會返回一個新的哈希,它將打印在下面我想將這些散列存儲到另一個散列中。 group1group2group3是哈希鍵:無法在Ruby中創建散列哈希

"group1" 
{ 
    "path"=>"/tmp/", 
    "max_hits"=>262144, 
    "min_hits"=>5240 
} 

"group2" 
{ 
    "basic_size_limit"=>262144, 
    "path"=> "/usr/bin/ruby", 
    "max_hits"=>2412, 
    "min_hits"=>871 
} 

"group3" 
{ 
    "path"=>"/usr/tmp/", 
    "basic_time_limits"=>[26214400, 26214401, 26214402], 
} 

我初始化上述哈希作爲groups = Hash.new,而不是我想將它們存儲到哈希散列。我想初始化更大的哈希像group_of_groups = Hash.new{|hash, key| hash[key] = Hash.new}

group_of_groups = Hash.new{|hash, key| hash[key] = Hash.new} 
Loop i in 1 .. 3 
    groups = Hash.new 
    #### 
     Do something and add to the hash groups 
    #### 
    key_name = "group" + i 
    group_of_groups[key_name] = groups 
end Loop 

但它只是給了我{"group1"=>{}, "group2"=>{}, "group3"=>{}}

如何解決這個問題?

+1

目前還不清楚重視你已經離開。個人散列中的3個羣體是否是變量?請編輯以顯示更多真實的Ruby代碼並展示這3個哈希的源代碼(我也看到''path'=「/ usr/bin/ruby​​」'帶有不正確的運算符'='而不是'=>') –

+0

這些是我印刷的三種不同的哈希值。我糾正了'='n'=>'的錯誤。我在這裏提供更多信息 –

+0

我明白它們是3種不同的哈希值,但它們是以變量存儲的,還是以Array的形式存儲? –

回答

3

如果你有3個變量組1的3個哈希值,組2,&組3,你可以做一個哈希值,像這樣的哈希值:

group1 = { 
    "path"=>"/tmp/", 
    "max_hits"=>262144, 
    "min_hits"=>5240 
} 

group2 = { 
    "basic_size_limit"=>262144, 
    "path"=> "/usr/bin/ruby", 
    "max_hits"=>2412, 
    "min_hits"=>871 
} 

group3 = { 
    "path"=>"/usr/tmp/", 
    "basic_time_limits"=>[26214400, 26214401, 26214402], 
} 

group_of_groups = {"group1"=>group1, "group2"=>group2, "group3"=>group3} 

# result 
{ 
    "group1" => { 
     "path"=>"/tmp/", 
     "max_hits"=>262144, 
     "min_hits"=>5240 
    }, 
    "group2" => { 
     "basic_size_limit"=>262144, 
     "path"=> "/usr/bin/ruby", 
     "max_hits"=>2412, 
     "min_hits"=>871 
    }, 
    "group3" => { 
     "path"=>"/usr/tmp/", 
     "basic_time_limits"=>[26214400, 26214401, 26214402], 
    }, 
} 

請記住,一切都在Ruby是通過引用,所以修改group_of_groups內部的散列也將修改原始散列。

+1

您的回答給了我解決方案的提示。我實際上是在每個循環結束時清除哈希,因此它結束了,但由於通過引用傳遞而在各處被刪除! –

2

如果你想一次初始化一切,你可以寫爲:

group_of_groups = { 
    "group1" => { 
     "path"=>"/tmp/", 
     "max_hits"=>262144, 
     "min_hits"=>5240 
    }, 
    "group2" => { 
     "basic_size_limit"=>262144, 
     "path"=> "/usr/bin/ruby", 
     "max_hits"=>2412, 
     "min_hits"=>871 
    }, 
    "group3" => { 
     "path"=>"/usr/tmp/", 
     "basic_time_limits"=>[26214400, 26214401, 26214402], 
    } 
} 

換行符弄清楚什麼是屬於什麼,但不是必需的。

0

除了其他的答案,你也可以做到這一點,如果你需要以編程方式生成散列:

group1 = { 
    "path"=>"/tmp/", 
    "max_hits"=>262144, 
    "min_hits"=>5240 
} 

group2 = { 
    "basic_size_limit"=>262144, 
    "path"=> "/usr/bin/ruby", 
    "max_hits"=>2412, 
    "min_hits"=>871 
} 

group3 = { 
    "path"=>"/usr/tmp/", 
    "basic_time_limits"=>[26214400, 26214401, 26214402], 
} 

group_of_groups = 
    ["group1", "group2", "group3"].each_with_object({}) do |sub_hash_name, new_hash| 
     new_hash[sub_hash_name] = instance_eval(sub_hash_name) 
    end 

# Result 
{ 
    "group1" => { 
     "path"=>"/tmp/", 
     "max_hits"=>262144, 
     "min_hits"=>5240 
    }, 
    "group2" => { 
     "basic_size_limit"=>262144, 
     "path"=> "/usr/bin/ruby", 
     "max_hits"=>2412, 
     "min_hits"=>871 
    }, 
    "group3" => { 
     "path"=>"/usr/tmp/", 
     "basic_time_limits"=>[26214400, 26214401, 26214402], 
    }, 
} 
+1

在這種情況下使用eval是不合適或不可取的。 – Gravatite

+0

@Gravatite因爲它很脆弱 - 如果有人決定更改局部變量的名稱,他們可能不知道更改數組的字符串值? – josiah

+1

有兩個原因。首先,Ruby支持正確的反射,所以沒有理由使用eval來獲取變量。第二個問題更多的是一個哲學問題,因爲eval通常會導致各種來自安全漏洞的惡果,難以診斷錯誤和所有其他問題。快速的谷歌搜索應該提供更多深入的文章,介紹何時適合使用eval。 – Gravatite