2013-03-30 157 views
2

在这里,红宝石更改哈希是我的示例程序:使用枚举

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 

,不改变原:

回答

5

如果你想变异到位哈希(你似乎想),只要做到这一点

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!在单个步骤中破坏性地改变散列。


还要注意的是,如果你想破坏性变异散列或引用其他可变对象,在地方,你可以只破坏性正常的迭代过程中发生变异的那些对象的任何其他对象:

# 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"} 

然而,因为你在示例代码中使用的是符号 - 因为符号是而不是可变 - 这个最后的注释并不直接适用于那里。

1

相反的:

w[1] = correction.chop!.to_sym 

尽量分配给直接哈希:

what[w[0]] = correction.chop!.to_sym 

红宝石创建w阵只是为了打发你的键和值。分配给该数组不会改变你的散列;它只是改变那个临时阵列。