Created
March 7, 2017 04:47
-
-
Save itzsaga/6f266365a3d1c4e94f9dcc7f77148d38 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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