I want to iterate an array, modify its element based on one criteria and want to insert another element after each element, except after the last. What would be the most Ruby-idiomatic way to do so?
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
Example:
transform("Hello case1 world!") => ["add theseHello", "x", "add that", "x", "add theseworld!"]
Making some assumptions about desired output, and editting: