在這裏,紅寶石更改哈希是我的示例程序:使用枚舉
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
,不改變原: