0
我重新定义了Array#replace
,如下所示。Array.each_with_index未按预期方式运行
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
是['html', 'ruby', 'ruby']
。我不知道book_topic
的结果。谁能告诉我为什么?
ohhhhhh。我很愚蠢......是的,我知道我在压倒核心课。非常非常感谢你! – madper 2013-04-28 15:33:51
@madper根据您的评论,您应该将其标记为答案。 – 2013-04-28 16:18:56
@CharlesCaldwell,当然。对不起我迟到了。当我把这个标记为答案时,它说我应该等9分钟。过了一段时间我就忘记了。我想我得了阿尔茨海默病。 :-) – madper 2013-04-29 02:05:56