A
Aldric Giacomoni
This took me less than a minute to write, but I don't know if it's as
elegant as it could be. Are there more "Ruby-like ways(tm)" to write
this?
def stringify array
return array[0] if array.size == 1
string = ""
array.each_with_index do |element, index|
case index
when array.size - 1 # last item
string += " and #{element}"
when 0
string += "#{element}"
else
string += ", #{element}"
end
end
string
end
elegant as it could be. Are there more "Ruby-like ways(tm)" to write
this?
def stringify array
return array[0] if array.size == 1
string = ""
array.each_with_index do |element, index|
case index
when array.size - 1 # last item
string += " and #{element}"
when 0
string += "#{element}"
else
string += ", #{element}"
end
end
string
end