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 :

RAniDBTools.format_episodes_list([ ‘1’, ‘2’, ‘3’, ‘4’, ‘6’, ‘7’, ‘9’, ‘S1’, ‘S2’ ])

=> “1-4, 6-7, 9, S1-S2”RAniDBTools.format_episodes_list([ ‘1’, ‘2’, ‘S3’, ‘S4’, ‘S5’, ‘O6’ ])

=> “1-2, S3-S5, O6”

Here’s the code ; what can I do to improve this ?

module RAniDBTools

def RAniDBTools.format_episodes_list(list)

lt = []

le = []

list.each do |epno|

if (‘0’…‘9’).include? epno[0,1]

t = ‘’

e = epno.to_i

else

t = epno[0…0]

e = epno[1…-1].to_i

end

if lt.last == t

max = le.last.max rescue le.last

min = le.last.min rescue le.last

if e == max + 1

le[-1] = (min…e)

else

le << e

lt << t

end

else

le << e

lt << t

end

end

f = []

le.each_with_index do |e, i|

if e.is_a? Range

f << “#{lt[i]}#{e.min}-#{lt[i]}#{e.max}”

else

f << “#{lt[i]}#{e}”

end

end

f.join(’, ')

end

end

TIA,

Fred