这是我的示例程序:
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
如果你想创建一个新的散列,而不改变原来的散列:
new_hash = Hash[ my_hash.map do |key,value|
[ key, new_value ]
end ]
它的工作方式是 Enumerable#map
返回一个数组(在本例中是一个包含两个元素的键/值对的数组),并且 Hash.[]
可以将 [ [a,b], [c,d] ]
转换为 { a=>b, c=>d }
。
您所做的——hash.map{ … }
——是将每个键/值对映射到一个新值并创建一个数组……然后对该数组不做任何操作。虽然 Array#map!
会破坏性地改变数组,但没有等效的 Hash#map!
来破坏性地改变哈希一步到位。
另请注意,如果您想破坏性地改变一个 Hash — 或引用其他可变对象的任何其他对象 — 您可以在正常迭代期间破坏性地改变这些对象:
# A simple hash with mutable strings as values (not symbols)
h = { a:"zeroth", b:"first", c:"second", d:"third" }
# Mutate each string value
h.each.with_index{ |(char,str),index| str[0..-3] = index.to_s }
p h #=> {:a=>"0th", :b=>"1st", :c=>"2nd", :d=>"3rd"}
但是,由于您在示例代码中使用符号表示值——并且由于符号不可变——最后的注释并不直接适用于此。
关于ruby - 使用枚举器更改 ruby 中的哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15714688/