Skip to content

Instantly share code, notes, and snippets.

@DGaffney
Created June 6, 2014 16:05
Show Gist options
  • Save DGaffney/5aa27abc7cace860e1b9 to your computer and use it in GitHub Desktop.
Save DGaffney/5aa27abc7cace860e1b9 to your computer and use it in GitHub Desktop.
class Array
def join_with_oxford_comma(delimiter=", ")
string = ""
if self.length == 1
string = self.first
elsif self.length == 2
string = self.join(" and ")
else
self.each_with_index do |elem,i|
if i == 0
string+= elem.to_s
elsif i == self.length-1
string+="#{delimiter}and #{elem}"
else
string+="#{delimiter}#{elem}"
end
end
end
string
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment