为什么这样工作?我认为这与通过引用/值传递有关,但事实并非如此。它是否与新的块范围有关?Ruby 1.8 vs 1.9 - 破坏性拒绝!运营商
def strip_ids(array)
array.each{ |row| row.reject! {|k, v| k =~ /_id/ } }
end
class Foo
attr_accessor :array
def initialize
@array = []
@array << { :some_id => 1, :something => 'one' }
@array << { :some_id => 2, :something => 'two' }
@array << { :some_id => 3, :something => 'three'}
end
end
foo = Foo.new
puts strip_ids(foo.array).inspect
puts foo.array.inspect
##########################
#
# Output in ruby 1.8.7
#
# [{:some_id=>1, :something=>"one"}, {:some_id=>2, :something=>"two"}, {:some_id=>3, :something=>"three"}]
#
#
# Output in ruby 1.9.1
#
# [{:something=>"one"}, {:something=>"two"}, {:something=>"three"}]
#
这是第一个。在1.8.7中做'k.to_s',你会没事的。 – 2009-11-04 04:28:36
实际上,我终于开始构建1.9.1p243,它也产生了一个符号,所以它必须是第二种解释:正则表达式现在适用于符号。但正如我们所指出的那样,解决方案也是一样的。 – DigitalRoss 2009-11-04 05:47:15
罗斯先生说得对。 – 2009-11-04 06:03:45