Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Kevin

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Write out an array as a list with commas and an 'and'

Actually, you can customize the separators to your needs.

   1  
   2  def text_list(listtext,sep1=", ", sep2=", and ")
   3    n=listtext.size
   4    if n>1 : (listtext.first(n-1)).join(sep1) + sep2 +listtext.last 
   5    else listtext.first end
   6  end
   7  
   8  text_list(["cat", "dog", "bird"]) => "cat, dog, and bird"
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS