a = [["John", "Doe"], ["Sue", "Smith"], ["Melba", "Jones"]]
The obvious way of doing this, that has been mentioned by others, is:
a.map { |arr| arr.join(' ') }.join(', ')
#=> "John Doe, Sue Smith, Melba Jones"
As an exercise, here are three ways this can be done without using Array#map
Use Enumerable#reduce (aka inject)
a.drop(1).reduce(a.first.join(' ')) { |s,name| s + ", %s %s" % name }
#=> "John Doe, Sue Smith, Melba Jones"
Use recursion
def doit((name, *rest))
rest.empty? ? name.join(' ') : "%s %s, %s" % [*name, doit(rest)]
end
doit(a)
#=> "John Doe, Sue Smith, Melba Jones"
Flatten, join with a space, use String#gsub to insert commas
r = /
\w+[ ]\w+ # match two words separated by a space
(?=[ ]) # positive lookahead asserts that next character is a space
\K # reset start of match to current location and discard all
# previously matched characters from match that is returned
/x # free-spacing regex definition mode
a.flatten.join(' ').gsub(r, ',')
#=> "John Doe, Sue Smith, Melba Jones"