R
Ruby Quiz
The three rules of Ruby Quiz:
1. Please do not post any solutions or spoiler discussion for this quiz until
48 hours have passed from the time on this message.
2. Support Ruby Quiz by submitting ideas as often as you can:
http://www.grayproductions.net/ruby_quiz/
3. Enjoy!
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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.
Here are some examples of possible usage:
lucky = Regexp.build( 3, 7 )
"7" =~ lucky # => true
"13" =~ lucky # => false
"3" =~ lucky # => true
month = Regexp.build( 1..12 )
"0" =~ month # => false
"1" =~ month # => true
"12" =~ month # => true
day = Regexp.build( 1..31 )
"6" =~ day # => true
"16" =~ day # => true
"Tues" =~ day # => false
year = Regexp.build( 98, 99, 2000..2005 )
"04" =~ year # => false
"2004" =~ year # => true
"99" =~ year # => true
num = Regexp.build( 0..1_000_000 )
"-1" =~ num # => false
Some issues you may want to consider while building you're library:
* How should leading zeros be handled?
Match the hour from a clock formatted in military time (0 to 23). Hours 0
through 9 may or may not have a single leading zero.
* Should anything be captured by the returned Regexp?
* How should anchoring work?
"2004" =~ Regexp.build( 4 ) # => ???
1. Please do not post any solutions or spoiler discussion for this quiz until
48 hours have passed from the time on this message.
2. Support Ruby Quiz by submitting ideas as often as you can:
http://www.grayproductions.net/ruby_quiz/
3. Enjoy!
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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.
Here are some examples of possible usage:
lucky = Regexp.build( 3, 7 )
"7" =~ lucky # => true
"13" =~ lucky # => false
"3" =~ lucky # => true
month = Regexp.build( 1..12 )
"0" =~ month # => false
"1" =~ month # => true
"12" =~ month # => true
day = Regexp.build( 1..31 )
"6" =~ day # => true
"16" =~ day # => true
"Tues" =~ day # => false
year = Regexp.build( 98, 99, 2000..2005 )
"04" =~ year # => false
"2004" =~ year # => true
"99" =~ year # => true
num = Regexp.build( 0..1_000_000 )
"-1" =~ num # => false
Some issues you may want to consider while building you're library:
* How should leading zeros be handled?
Match the hour from a clock formatted in military time (0 to 23). Hours 0
through 9 may or may not have a single leading zero.
* Should anything be captured by the returned Regexp?
* How should anchoring work?
"2004" =~ Regexp.build( 4 ) # => ???