ruby - 使用枚举器更改 ruby​​ 中的哈希

标签 ruby hash dictionary block

这是我的示例程序:

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/

相关文章:

python:查找围绕某个 GPS 位置的圆的 GPS 坐标的优雅方法

python - 如何检查字典是否为空并且字典类似于 {"":""} 在 python 中

ruby - 适用于 Ruby 的强大 TCP 服务器?

ruby - 在 rake 中,如何创建一个包含文件但在包内重命名的包

algorithm - 树结构哈希

hadoop - 分区! hadoop是怎么做到的?使用哈希函数?默认功能是什么?

c++ - 散列 2D 点的有效方法

shell - 如果xargs是map,那么filter是什么?

ruby - 解析不断增长的日志文件

ruby-on-rails - 使用字符串作为类关联名称 Rails