How can I add a prefix to all array elements except the first and last ones?
Is there a one line solution to that? Or a ‘nicer’ solution than mine?
result = ["svn up", "20111128025010", "20111128025022", "pass"]
for i in 1..result.length-2
result[i]=" - "+result[i]
end
You could do it like this:
Note that there are three dots, not two. This works because
s[0,0]modifiessin-place. If you’re using 1.9.3+, you could also (and probably should) useprepend:And, as the Tin Man notes in the comments, you could also use the double-dot range operator which is more common:
Which range operator you use is a matter of preference.