From: Marnen Laibow-Koser Date: 2009-12-11T02:49:19+09:00 Subject: Re: human-readable listing of array elements Benoit Daloze wrote: > Hi, > > As written in "Programming Ruby 1.9", > You can really use case as if ... elsif ... else ... end > > So, in our case: > > 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 > > becomes: > > case > when self.size <= 1 > return self.to_s > when 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 > Thanks, I had forgotten about that too. > Well, that's not especially better I think, but it looks cool and a > little > less 'procedural' to me. It's no less procedural, and in fact it uses another line of code for no advantage that I can see. If you truly want this to look less procedural, I think you'd need to somehow implement a polymorphic dispatch based on Array.size...but let's not go there. :) > > Anyway, I'm wondering how case manage the comma(,), is it acting like a > OR > on each of the elements? I thought first to Array#=== but that isn't > defined. So, is it sort of syntactic sugar ? > > 2009/12/10 Robert Klemme Best, --  Marnen Laibow-Koser http://www.marnen.org marnen@marnen.org -- Posted via http://www.ruby-forum.com/.