On 8/25/06, Ruby Quiz <james / grayproductions.net> wrote:
> The purpose of this quiz is to find the best "Ruby way" to generate this
> sentence-like string.

Here's my short solution.
It uses the values from Time.strftime to generate abbreviations.
t handles input as a list of numbers, or comma separated strings, or a
mixture of both.
For example
p DayRange.new(1,"Tuesday","Wed",4,"Sat,Sun").to_s #=>"Mon-Thu, Sat, Sun"


-------
class DayRange
  def initialize *l
    dnames = (1..7).map{|i|Regexp.new(dayname(i).slice(0,3),Regexp::IGNORECASE)}<</.*/

    l=l.map{|v|v.respond_to?(:split) ? v.split(',') : v}.flatten
    @range=l.map{|v|
      (n=v.to_i)>0 ? n : (1..8).find{|i|dnames[i-1]=~v.to_s}}.sort
    raise "ArgumentError" if @range[-1]>7
  end
  def dayname n
    Time.gm(1,1,n).strftime("%a")
  end
  def to_s
    l=9
    s = @range.map{|e|"#{"-" if e==l+1}#{dayname(l=e)}"}.join(', ')
    s.gsub(/(, -\w+)+, -/,'-').gsub(/ -/,' ')
  end
end