编辑另一个哈希中包含的哈希

时间:2014-07-30 10:22:58

标签: ruby dictionary hashmap

我在Hash中有一个Hash,具有以下结构:

mysystem = {
  "slicompany" => {
    "sahil" => "developer",
    "Jag" => "developer"
  },
  "uzanto" => {
    "kapil" => "tech lead"
  }
}

添加/删除/编辑内部哈希元素的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

正如Uri Agassi所说,通过按键访问嵌套哈希非常容易......

mysystem['slicompany']['sahil'] = 'tech lead'

为方便起见,您还可以将内部哈希值分配给单独的变量,对该变量的更改将反映在"外部"变量(因为内部变量和外部变量的散列指向同一个散列对象)

slicompany = mysystem['slicompany']

slicompany["George"] = "dishwasher"

p mysystem
=> {"slicompany" => {"sahil" => "developer", "Jag" => "developer", "George" => "dishwasher"},
  "uzanto" => {"kapil" => "tech lead"}}