In article <20041015131345.FPZO6435.lakermmtao04.cox.net / localhost.localdomain>,
  Ruby Quiz <james / grayproductions.net> writes:

> There's been some discussion on Ruby Talk lately about Range.member? which tests
> if a given element (often a number) is a member of the set the Range object
> iterates over.  Obviously, this kind of test is useful in many aspects of
> programming, but let's approach this problem from a different angle.
>
> This week's quiz is to build a library that adds a class method called build()
> to Regexp.  build() should accept a variable number of arguments which can
> include integers and ranges of integers.  Have build() return a Regexp object
> that will match only integers in the set of passed arguments.

def Regexp.build(*args)
  args = args.map {|arg| Array(arg) }.flatten.uniq.sort
  neg, pos = args.partition {|arg| arg < 0 }
  /\A(?:-0*#{Regexp.union(*neg.map {|arg| (-arg).to_s })}|0*#{Regexp.union(*pos.map {|arg| arg.to_s })})\z/
end

It use Regexp.union which is introduced at Ruby 1.8.1.

Since it expands all ranges, long ranges such as
Regexp.build( 0..1_000_000 ) works slowly, though.
-- 
Tanaka Akira