Conjoining an array in English
arrays, ruby, string
Solution
Yep.
require 'active_support/core_ext/array/conversions'
["Peter", "Paul", "Mary"].to_sentence
=> "Peter, Paul, and Mary"
or without an oxford comma.
["Peter", "Paul", "Mary"].to_sentence(:last_word_connector => ' and ')
=> "Peter, Paul and Mary"
Here's more info from apidock
Also note, that if you're using rails, at least, you can control this from a language file using i18n to avoid duplicating, or adding complexity to, this logic.
Problem
Is there a ready made library/method that serializes an array into a string as follows? ``` [] #=> "" ["Peter"] #=> "Peter" ["Peter", "Paul"] #=> "Peter and Paul" ["Peter", "Paul", "Mary"] #=> "Peter, Paul, and Mary" (with Oxford comma) ["Peter", "Paul", "Mary"] #=> "Peter, Paul and Mary" (without Oxford comma) ``` If not, what is the shortest way to do this? I would like it in this form: ``` class Array def conjoin oxford_comma = true ... end end ```