我有一个由变量组成的数组,我想对每个变量执行相同的操作,并将结果存储在原始变量中:
(one, two, three) = [1, 2, 3]
[one, two, three].map!{|e| e += 1}
# => [2, 3, 4]
# But:
[one, two, three]
# => [1, 2, 3]
# You have to:
(one, two, three) = [one, two, three].map{|e| e += 1}
# => [2, 3, 4]
[one, two, three]
# => [2, 3, 4]
这似乎不是做到这一点的“正确方法”,但我没有设法找到那种“正确的方法”。我对发生的事情也有一些模糊的想法,但我不太确定,因此将不胜感激。
我的实际用例是我有命名参数,我是e = File.new(e) if e.is_a? String