From: Ken Bloom Date: 2006-08-29T05:00:11+09:00 Subject: Re: [QUIZ] [SOLUTION] DayRange (#92) My second solution builds off of seeing one line of Morus' solution: Morus Walter wrote: > self.inject( [ [] ] ) do | list, item | (which helped, because I rarely remember inject) and a new solution was born: class DayRange NAMEMAP={"Mon"=>1, "Tue"=>2, "Wed"=>3, "Thu"=>4, "Fri"=>5, "Sat"=>6, "Sun"=>7, "Thurs"=>4, "Monday"=>1, "Tuesday"=>2, "Wednesday"=>3, "Thursday"=>4, "Friday"=>5, "Saturday"=>6, "Sunday"=>7} REVERSENAMEMAP=[nil, "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] def initialize(*args) #parse arguments into Integers from 1 to 7 args.collect!{|x| NAMEMAP[x] || x} args.sort!.uniq! raise ArgumentError if args.any? do |x| not x.is_a?(Fixnum) or not (1..7).include? x end #turn everything into ranges @ranges=args.inject([]) do |a,v| if a[-1]==nil or a[-1].last != v-1 a << (v..v) else #extend the existing range to include the new element a[-1]=((a[-1].first)..v) end a end #this code can be included if you would like wrap-around ranges #note that it constructs an ranges (with last