如何更新嵌套在哈希中的数组?

时间:2015-06-05 00:25:27

标签: arrays ruby hashmap

我的脚本有:

my_hash = {'key1' => 'value1', 'key2' => ['value2', 'value3']}
my_array = ["text...#{variable1}", "text...#{variable2}", "text...#{variable3}"]
my_hash['key2'] = my_array

我收到:[]=': can't convert Hash into String (TypeError)

如何更新key2的整个数组?

1 个答案:

答案 0 :(得分:1)

new_array = ['value5']

hash = {
  'key1' => 'value1',
  'key2' => ['value2', 'value3']
}

hash['key2'] = new_array