在这里,红宝石更改哈希是我的示例程序:使用枚举
what = {:banana=>:fruit, :pear=>:fruit, :sandal=>:fruit, :panda=>:fruit, :apple=>:fruit}
what.map do |w|
p "is this right?"
awesome_print w
fix = gets
fix.chop!
if (fix == "N")
p "Tell me what it should be"
correction = gets
w[1] = correction.chop!.to_sym
end
p w
end
我运行它,我得到的(包括我输入)这样的:
"is this right?"
[
[0] :banana,
[1] :fruit
]
Y
[:banana, :fruit]
"is this right?"
[
[0] :pear,
[1] :fruit
]
Y
[:pear, :fruit]
"is this right?"
[
[0] :sandal,
[1] :fruit
]
N
"Tell me what it should be"
footwear
[:sandal, :footwear]
"is this right?"
[
[0] :panda,
[1] :fruit
]
N
"Tell me what it should be"
animal
[:panda, :animal]
"is this right?"
[
[0] :apple,
[1] :fruit
]
Y
[:apple, :fruit]
=> [[:banana, :fruit], [:pear, :fruit], [:sandal, :footwear], [:panda, :animal], [:apple, :fruit]]
>> what
=> {:banana=>:fruit, :pear=>:fruit, :sandal=>:fruit, :panda=>:fruit, :apple=>:fruit}
我的问题是如何能我改变哈希?当我运行程序时,irb告诉我每个枚举元素都被处理,但结果不会保存在我的散列表what
中。如果你想创建一个新的哈希
my_hash.each do |key,value| # map would work just as well, but not needed
my_hash[key] = some_new_value
end
,不改变原: