From: Robert Klemme Date: 2010-04-04T19:20:05+09:00 Subject: Re: combined ranges... On 04/03/2010 03:07 AM, Caleb Clausen wrote: > On 4/2/10, Roger Pack wrote: >> Anybody know if there's an easy way to accomplish the equivalent of >> this: >> >> [1..20, 30..46].each{|n| >> # n = 1,2,3,4,5..30, 31, 32 >> } >> >> ? > > Oh, I wish for a real integer set class! Well, there's Fixnum / Bignum: irb(main):004:0> set = 0 => 0 irb(main):005:0> set[1] => 0 irb(main):006:0> set |= (1 << 1) => 2 irb(main):007:0> set[1] => 1 irb(main):008:0> set |= (1 << 100) => 1267650600228229401496703205378 irb(main):009:0> set[1] => 1 irb(main):010:0> set[100] => 1 It's fairly easy to build something around that, e.g. class IntSet include Enumerable def initialize @s = 0 end def add? x @s[x] == 0 and @s |= (1 << x) end def add x add? x self end alias << add def delete? x @s[x] == 1 and @s ^= (1 << x) end def include? x @s[x] == 1 end def each x = 0 it = 1 << x while it <= @s yield x if @s[x] == 1 x += 1 it <<= 1 end self end end is = IntSet.new is << 10 << 5 << 14 is.each {|x| p x} Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/