I like to join an array resulting in an 'English list'. For example ['one', 'two', 'three']
should result in 'one, two and three'
.
I wrote this code to achieve it (assuming that the array is not empty, which is not the case in my situation)
if array.length == 1
result = array[0]
else
result = "#{array[0, array.length].join(', ')} and #{array.last}"
end
But I was wondering whether there exists some 'advanced' join method to achieve this behaviour? Or at least some shorter/nicer code?