嵌套哈希迭代:如何在哈希中的((哈希数组)上迭代合并)

时间:2013-09-19 20:14:32

标签: ruby arrays hash nested

正如标题所说,我正在努力做到。这是我的代码:

school.each { |x| school[:students][x].merge!(semester:"Summer") }

我想我已经将问题确定为上面的“[x]”。如果我替换一个数组位置,如“[2]”,它工作正常。如何使迭代工作?

如果上述信息不足或您想提供更好的解决方案,请参阅以下详细信息。谢谢!


我得到的错误消息:

file.rb:31:在[]': no implicit conversion of Array into Integer (TypeError) from file.rb:31:in块中'     来自file.rb:31:in each' from file.rb:31:in'

更改前的嵌套哈希:

school = { 
  :name => "Happy Funtime School",
  :location => "NYC",
  :instructors => [ 
    {:name=>"Blake", :subject=>"being awesome" },
    {:name=>"Ashley", :subject=>"being better than blake"},
    {:name=>"Jeff", :subject=>"karaoke"}
  ],
  :students => [ 
    {:name => "Marissa", :grade => "B"},
    {:name=>"Billy", :grade => "F"},
    {:name => "Frank", :grade => "A"},
    {:name => "Sophie", :grade => "C"}
  ]
}

我正在尝试追加:semester =>“夏天”到最后四个哈希中的每一个。这是我想要的:

# ...preceding code is the same. Changed code below...
  :students => [ 
    {:name => "Marissa", :grade => "B", :semester => "Summer"},
    {:name=>"Billy", :grade => "F", :semester => "Summer"},
    {:name => "Frank", :grade => "A", :semester => "Summer"},
    {:name => "Sophie", :grade => "C", :semester => "Summer"}
  ]
}

3 个答案:

答案 0 :(得分:1)

迭代学生:

school[:students].each { |student| student[:semester] = "Summer" }

或者,使用合并:

school[:students].each { |student| student.merge!(semester: "Summer") }

答案 1 :(得分:0)

问题是当你做array.each {| x |做某事},x实际上是指数组中的每个元素。

例如,在循环的第一次迭代中,

x = {:name => "Marissa", :grade => "B"}

所以你真正在做的是尝试引用:

school[:student][{:name => "Marissa", :grade => "B"}]

哪个不起作用

你可以做的是创建一个for循环来跟踪索引。

for i in 0 ... school[:student].count
    school[:students][i].merge!(semester:"Summer")
end

编辑:Stefan的解决方案比我的解决方案要好得多,但我会把它留下来显示你出错的地方。

答案 2 :(得分:0)

我会使用Hash#store

执行以下操作
require 'awesome_print'

school = {
                 :name => "Happy Funtime School",
             :location => "NYC",
          :instructors => [
                    {
                                 :name => "Blake",
                              :subject => "being awesome"
                    },
                    {
                                 :name => "Ashley",
                              :subject => "being better than blake"
                    },
                    {
                                 :name => "Jeff",
                              :subject => "karaoke"
                    }
          ],
             :students => [
                    {
                               :name => "Marissa",
                              :grade => "B"
                    },
                    {
                               :name => "Billy",
                              :grade => "F"
                    },
                    {
                               :name => "Frank",
                              :grade => "A"
                    },
                    {
                               :name => "Sophie",
                              :grade => "C"
                    }
          ]
}

school[:students].each{|h| h.store(:semester ,"Summer")}
ap school,:index => false,:indent => 10

<强>输出

{
                 :name => "Happy Funtime School",
             :location => "NYC",
          :instructors => [
                    {
                                 :name => "Blake",
                              :subject => "being awesome"
                    },
                    {
                                 :name => "Ashley",
                              :subject => "being better than blake"
                    },
                    {
                                 :name => "Jeff",
                              :subject => "karaoke"
                    }
          ],
             :students => [
                    {
                                  :name => "Marissa",
                                 :grade => "B",
                              :semester => "Summer"
                    },
                    {
                                  :name => "Billy",
                                 :grade => "F",
                              :semester => "Summer"
                    },
                    {
                                  :name => "Frank",
                                 :grade => "A",
                              :semester => "Summer"
                    },
                    {
                                  :name => "Sophie",
                                 :grade => "C",
                              :semester => "Summer"
                    }
          ]
}