配列を反復処理し、1つの基準に基づいてその要素を変更し、最後の要素の後を除いて、各要素の後に別の要素を挿入したいと思います。これを行うための最もRubyの慣用的な方法は何でしょうか?
def transform(input)
words = input.split
words.collect {|w|
if w == "case1"
"add that"
else
"add these" + w
end
# plus insert "x" after every element, but not after the last
}
end
例:
transform("Hello case1 world!") => ["add theseHello", "x", "add that", "x", "add theseworld!"]