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:
3. Enjoy!
Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone
on Ruby Talk follow the discussion. Please reply to the original quiz message,
if you can.
···
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
by Bryan Donovan
If you've ever created a web application that deals with scheduling recurring
events, you may have found yourself creating a method to convert a list of days
into a more human-readable string.
For example, suppose a musician plays at a certain venue on Monday, Tuesday,
Wednesday, and Saturday. You could pass a list of associated day numbers to your
object or method, which might return "Mon-Wed, Sat".
The purpose of this quiz is to find the best "Ruby way" to generate this
sentence-like string.
Basically, the rules are:
* The class's constructor should accept a list of arguments that can be day
numbers (see day number hash below), day abbreviations ('Mon', 'Tue', etc.),
or the full names of the days ('Monday', 'Tuesday', etc.).
* If an invalid day id is included in the argument list, the constructor
should raise an ArgumentError.
* The days should be sorted starting with Monday.
* Three or more consecutive days should be represented by listing the first
day followed by a hyphen (-), followed by the last day of the range.
* Individual days and the above day ranges should be separated by commas.
* The class should number days (accepting Integers or Strings) as follows:
1: Mon
2: Tue
3: Wed
4: Thu
5: Fri
6: Sat
7: Sun
* The class needs a method named #to_s that returns the day range string.
Here are some example lists of days and their expected returned strings:
1,2,3,4,5,6,7: Mon-Sun
1,2,3,6,7: Mon-Wed, Sat, Sun
1,3,4,5,6: Mon, Wed-Sat
2,3,4,6,7: Tue-Thu, Sat, Sun
1,3,4,6,7: Mon, Wed, Thu, Sat, Sun
7: Sun
1,7: Mon, Sun
1,8: ArgumentError
This is not intended to be a difficult quiz, but I think the solutions would be
useful in many situations, especially in web applications. The solution I have
come up with works and is relatively fast (fast enough for my purposes anyway),
but isn't very elegant. I'm very interested in seeing how others approach the
problem.