0

以下のように再定義し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_topicis がアサートされ['html', 'ruby', 'ruby']ます。の結果についてはわかりませんbook_topic。誰でも理由を教えてもらえますか?

4

2 に答える 2