以下のように再定義し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
。誰でも理由を教えてもらえますか?