我重新定义Array#replace
如下。
require 'test/unit'
class Array
def replace (from, to)
each_with_index do |e, i|
self[i] = to if e = from
end
end
end
class TestDriver <Test::Unit::TestCase
def test_replace
book_topic = ['html', 'java', 'css']
book_topic.replace('java', 'ruby')
result_topic = ['html', 'ruby', 'css']
assert_equal book_topic, result_topic
end
end
当我运行该测试用例时,它断言book_topic
is ['html', 'ruby', 'ruby']
。我不知道book_topic
. 谁能告诉我为什么?