On 09/20/2010 11:06 AM, Joel VanderWerf wrote:
> ...
Refactoring that:
a = [ '1', '2', '3', '4', '6', '7', '9', 'S1', 'S2', "10", "11", "12" ]
#a = [ '1', '2', 'S3', 'S4', 'S5', 'O6' ]
module Enumerable
def each_run(cond)
run = nil
last = nil
each_cons 2 do |prev, s|
if cond[prev, s]
run ||= [prev]
run << s
else
yield run || prev
run = nil
end
last = s
end
yield run || [last]
self
end
end
result = []
a.each_run(proc {|prev, s| prev.succ == s}) do |run|
if run.size > 1
result << "#{run.first}-#{run.last}"
else
result << run.first
end
end
p result