From: Benoit Daloze Date: 2009-12-11T02:42:30+09:00 Subject: Re: human-readable listing of array elements --0016e659fcfc738779047a63541b Content-Type: text/plain; charset=ISO-8859-1 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 Well, that's not especially better I think, but it looks cool and a little less 'procedural' to me. 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 > 2009/12/10 Aldric Giacomoni : > > Marnen Laibow-Koser wrote: > >> > >> Yes. case won't handle <= conditions AFAIK. > > > > That is a very good point, though it does handle ranges, so "case 0..1" > > would fit this particular scenario. > > Your elsif statement is probably better suited here though. > > That point is not exactly true: while it doesn't out of the box, it > can easily be made to: > > irb(main):001:0> ONE_OR_LESS = lambda {|n| n <= 1} > => # > irb(main):002:0> class < => nil > irb(main):003:0> 5.times {|i| case i; when ONE_OR_LESS then puts > "yow!" else puts "nah" end} > yow! > yow! > nah > nah > nah > => 5 > irb(main):004:0> > > Apart from that, the length of an Array can as a minimum become only 0 > so the same test can be done as > > case array.size > when 0,1 # <= 1 > ... > when > ... > end > > Kind regards > > robert > > -- > remember.guy do |as, often| as.you_can - without end > http://blog.rubybestpractices.com/ > > --0016e659fcfc738779047a63541b--