无法在Ruby中创建哈希哈希

时间:2015-10-27 20:53:50

标签: ruby hash

循环在具有某些键值对的文本文件上运行三次。每个循环返回一个新的哈希,打印在下面。我想将这些哈希存储到另一个哈希中。 group2group3"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"=>{}}

但它只给了我@Html.Raw(myString)

如何解决这个问题?

3 个答案:

答案 0 :(得分:3)

如果3个变量中有3个哈希,则group1,group2和& group3,你可以像这样使用哈希进行哈希:

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],
    }
}

新行明确了什么属于什么,但不是必需的。

答案 2 :(得分: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],
    },
}