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

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

The constructor can take numbers, abbreviated names (Mon, Tue etc) and also=
=20
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 language=
 (I=20
coded for En, Fr, De, It, and Es). If you pass a non-nil value to the "form=
"=20
argument it will return full names rather than abbreviations.

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

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

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

  def to_s
    s =3D String.new

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

    s +=3D "#{@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] =3D=3D 1 and offset[i+1] =3D=3D 1 # Found a range?
          s +=3D "-" unless s[-1] =3D=3D 45 # "-"
          next                                 # then move along...
        else
          s +=3D " #{@day_str_array[v-1][@type]}" # otherwise add the name.
          next
        end
      else
        s +=3D " #{@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 =3D String.new
    @day_num_array.each { |n| s +=3D "#{@day_str_array[n-1][@type]} " }
    s.strip!
  end

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

  private
  def parse_args
    if @days[0].class =3D=3D Fixnum
      @day_num_array =3D @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 =3D 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','mi=E9rcoles'],['jue','jueves'=
],
     ['vie','viernes'],['sab','s=E1bado'],['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=EC'],['mar','marted=EC'],['mer','mercoled=EC'],['gio','g=
ioved=EC'],
     ['ven','venerd=EC'],['sab','sabato'],['dom','domenica']]
  end

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

=2Dd
=2D-=20
darren kirby :: Part of the problem since 1976 :: http://badcomputer.org
"...the number of UNIX installations has grown to 10, with more expected..."
=2D 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
vctNIMLmF6xUSE9mU6560LI=
=c/sF
-----END PGP SIGNATURE-----

--nextPart1398464.e2a1lfSOlH--