--nextPart1398464.e2a1lfSOlH
Content-Type: text/plain;
  charsetso-8859-1"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

I found this problem deceptively difficult. I had the bulk of the class 
written in a few minutes, and the other 90% of the time I spent on this was 
on the to_s method. I just couldn't figure it out. I finally came up with a 
solution, but I do not think it is particularly nice.

The constructor can take numbers, abbreviated names (Mon, Tue etc) and also 
full names. The only catch is that they must be passed in an array.

The "lang" argument allows you to return the result in a different languageI 
coded for En, Fr, De, It, and Es). If you pass a non-nil value to the "form" 
argument it will return full names rather than abbreviations.

If you want to change the "form" you must also pass the "lang". This is where 
having named args not dependant on position would be very useful...Why are 
there none?  

Anyway, here is a sample irb session followed by the code:
###################################
> days = DayRange.new([1,2,3,4,5,6,7])
...
> days.to_s
=> "Mon-Sun"
> days = DayRange.new(["Mon","Wed","Fri"], lang='en', form=1)
...
> days.to_s
=> "Monday, Wednesday, Friday"
> days = DayRange.new([1,5,6,7], lang='es')
...
> days.to_s
=> "lun, vie-dom"
> days = DayRange.new([1,4,5,6], lang='de', form=1)
...
> days.to_s
=> "Montag, Donnerstag-Samstag"
# add another language:
> class DayRange
1>   def day_no
2>      [['Man','Mandag'],['Tir','Tirsdag'],['Ons','Onsdag'],
['Tor','Torsdag'], ['Fre','Fredag'],['L','Ldag'],['S','Sdag']]
2>   end
1> end
=> nil
> days = DayRange.new([1,2,3,4,5,6,7], lang='no', form=1)
...
> days.to_str # different than 'to_s'
=> "Mandag Tirsdag Onsdag Torsdag Fredag Ldag Sdag"
...
###################################
# Quiz 92 - Day Range

class DayRange
  def initialize(days, lang='en', form=nil)
    form == nil ? @type = 0 : @type = 1 # Abbreviated or full name?
    @day_str_array = send "day_#{lang}" # 'lang' one of: en fr de es it
    @day_num_array = Array.new
    @days = days
    parse_args
  end

  def to_s
    s = String.new

    # Offset is the difference between numeric day values
    offset = Array.new
    f = @day_num_array[0]
    @day_num_array[1..-1].each do |n|
      offset << n - f
      f = n
    end

    s += "#{@day_str_array[@day_num_array[0]-1][@type]} "
    @day_num_array[1..-1].each_with_index do |v,i|
      if i < @day_num_array[1..-1].size
        if offset[i] == 1 and offset[i+1] == 1 # Found a range?
          s += "-" unless s[-1] == 45 # "-"
          next                                 # then move along...
        else
          s += " #{@day_str_array[v-1][@type]}" # otherwise add the name.
          next
        end
      else
        s += " #{@day_str_array[i][@type]}"
      end
    end
    # cleanup and return string
    s.gsub!(" -","-")
    s.gsub!("- ","-")
    s.gsub!(/ {2,}/," ")
    s.gsub!(" ",", ")
    s
  end

  # Maybe you just want the day names
  def to_str
    s = String.new
    @day_num_array.each { |n| s += "#{@day_str_array[n-1][@type]} " }
    s.strip!
  end

  # Maybe you want them in an array
  def to_a
    a = Array.new
    @day_num_array.each { |n| a << @day_str_array[n-1][@type] }
    a
  end

  private
  def parse_args
    if @days[0].class == Fixnum
      @day_num_array = @days.sort!
      if @day_num_array[-1] > 7
        raise ArgumentError, "Argument out of range: #{@day_num_array[-1]}"
      end
    else
      @days.each do |d|
        if @day_str_array.flatten.include?(d)
          indice = case @day_str_array.flatten.index(d)
            when 0..1:   1
            when 2..3:   2
            when 4..5:   3
            when 6..7:   4
            when 8..9:   5
            when 10..11: 6
            when 12..13: 7
          end
          @day_num_array << indice
        else
          raise ArgumentError, "Bad argument: #{d}"
        end
      end
      @day_num_array.sort!
    end
  end

  def day_en
    [['Mon','Monday'],['Tue','Tuesday'],['Wed','Wednesday'],
['Thu','Thursday'],
     ['Fri','Friday'],['Sat','Saturday'],['Sun','Sunday']]
  end

  def day_fr
    [['lun','lundi'],['mar','mardi'],['mer','mercredi'],['jeu','jeudi'],
     ['ven','vendredi'],['sam','samedi'],['dim','dimanche']]
  end

  def day_es
    [['lun','lunes'],['mar','martes'],['mie','micoles'],['jue','jueves'],
     ['vie','viernes'],['sab','sado'],['dom','domingo']]
  end

  def day_de
    [['Mon','Montag'],['Die','Dienstag'],['Mit','Mittwoch'],
['Don','Donnerstag'],
     ['Fre','Freitag'],['Sam','Samstag'],['Son','Sonntag']]
  end

  def day_it
    [['lun','luned],['mar','marted],['mer','mercoled],['gio','gioved],
     ['ven','venerd],['sab','sabato'],['dom','domenica']]
  end

end
##################################

-d
-- 
darren kirby :: Part of the problem since 1976 :: http://badcomputer.org
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972

--nextPart1398464.e2a1lfSOlH
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)

iD8DBQBE8hzlwPD5Cr/3CJgRAr1RAKCOkG20IGXhC50d8SENDdKn4FWVFACgxeF1
vctNIMLmF6xUSE9mU6560LIF
-----END PGP SIGNATURE-----

--nextPart1398464.e2a1lfSOlH--