From: Joel VanderWerf Date: 2010-09-21T03:06:36+09:00 Subject: Re: An elegant way... On 09/20/2010 10:10 AM, F. Senault wrote: > Hello everybody. > > I've written a method to solve a little problem, but I find my solution > really ugly. So, I'm trying to find ways to improve it. > > The method takes one argument, an array containing a sorted list of > strings representing episodes numbers. The episodes numbers are either > a number ('1', '12') or prefixed with a letter ('S1' for special 1). My > goal is to find sequences in the numbers and join them with dashes : a = [ '1', '2', 'S3', 'S4', 'S5', 'O6' ] r0 = nil r1 = nil result = [] a.each_cons 2 do |prev, s| case s when prev.succ r0 ||= prev r1 = s else if r0 result << "#{r0}-#{r1}" else result << prev end r0 = r1 = nil end end if r0 result << "#{r0}-#{r1}" else result << a.last end p result # ==> ["1-2", "S3-S5", "O6"]