Skip to content

Instantly share code, notes, and snippets.

@itzsaga
Created March 7, 2017 04:47
Show Gist options
  • Save itzsaga/6f266365a3d1c4e94f9dcc7f77148d38 to your computer and use it in GitHub Desktop.
Save itzsaga/6f266365a3d1c4e94f9dcc7f77148d38 to your computer and use it in GitHub Desktop.
def oxford_comma(array)
if array.length == 1
return "#{array[0]}"
elsif array.length == 2
return array.join(" and ")
elsif array.length >= 3
new_last_array_item = "and #{array[-1]}"
array.pop
array.push(new_last_array_item)
return array.join(", ")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment