From: Marnen Laibow-Koser Date: 2009-12-11T01:51:34+09:00 Subject: Re: human-readable listing of array elements Aldric Giacomoni wrote: > 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"? Yes. case won't handle <= conditions AFAIK. > 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. Use whichever syntax is better suited to what you need. There's nothing wrong with elsif IMHO. Best, --  Marnen Laibow-Koser http://www.marnen.org marnen@marnen.org -- Posted via http://www.ruby-forum.com/.