From: Julian Tarkhanov Date: 2007-10-01T07:14:07+09:00 Subject: Re: Detecting number ranges On 30-sep-2007, at 23:40, Jay Levitt wrote: > Thanks! For my own educational purposes (and any future newbies > reading > this), I renamed a bunch of variables, extracted some temp > variables and > came up with the following. It still has one bug (doesn't handle > non-contiguous ranges like [1, 2, 133]), but I think I could fix > that on my > own... Well I used the following snippet once module Enumerable def to_ranges compact.sort.uniq.inject([]) do | result, elem | result = [elem..elem] if result.length.zero? if [result[-1].end, result[-1].end.succ].include?(elem) result[-1] = result[-1].begin..elem else result.push elem..elem end result end end def member_includes?(item) find{|r| r.include?(item)} ? true : false end def implant(item) (map{|e| e.to_a }.flatten + Array(item)).to_ranges end def implant!(item) replace(implant(item)) end end You can basically make an array and the "collapse" it -- Julian 'Julik' Tarkhanov please send all personal mail to me at julik.nl