From: Aldric Giacomoni Date: 2009-12-11T00:52:46+09:00 Subject: Re: human-readable listing of array elements Marnen Laibow-Koser wrote: > > Interesting question. > > class Array # or perhaps module Enumerable > def stringify(conjunction = 'and', separator = ', ') > if self.size <= 1 > return self.to_s > elsif self.size == 2 > return self.join " #{conjunction} " > else > array = self.dup > array[-1] = [conjunction, array[-1]].join ' ' > array.join separator # proper English usage has comma before 'and' > end > end > end It is pretty, and (like Robert's solution) handles the case where the array has no elements. I wouldn't put that into Enumerable though, not sure how it would handle a hash, for instance :) Do you think if / elsif / else is better in this situation than "case self.size"? What about in general? I know it's usually accepted that instead of several elsifs, one should use 'case', but I usually try to avoid 'elsif' altogether. -- Posted via http://www.ruby-forum.com/.