Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Write Out An Array As A List With Commas And An 'and'

DZone's Guide to

Write Out An Array As A List With Commas And An 'and'

·
Free Resource
Actually, you can customize the separators to your needs.


def text_list(listtext,sep1=", ", sep2=", and ")
  n=listtext.size
  if n>1 : (listtext.first(n-1)).join(sep1) + sep2 +listtext.last 
  else listtext.first end
end

text_list(["cat", "dog", "bird"]) => "cat, dog, and bird"
Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}