Please Forward: Ruby Quiz Submission

I tried to send this before, but it didn't seem to go through. Trying again. Sorry if this is a duplicate.

James Edward Gray II


Begin forwarded message:

Date: August 29, 2006 5:37:24 AM CDT
Subject: Please Forward: Ruby Quiz Submission

Hi James. Here's my submission for number 92. Short, and reasonably sweet.


#a cute little helper method to do the hard work
class Array
  def split &blk
    #split the array into little arrays at boundary where block evals to true
    r = []
    while i<length
      loop do
        break if, at(i))
        if i==length-1
      r << slice(start..i-1)

# i personally don't really see the point of wrapping this up in a class
def dayrange *args
  r = []
  days = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun']
  args.collect!{|d| days.include?(d) ? days.index(d) : d }
  raise"invalid arguments #{args.inspect} for dayrange")
unless (args - (0..6).to_a).empty?
  args.sort.split{|a,b| a.succ != b}.each{|d|
    if d.size>=3
      r << days[d[0]] + '-' + days[d[-1]]
      r.concat d.collect{|n| days[n]}

#some examples
puts dayrange(0,2,3,4,6)
puts dayrange('Mon','Tue','Wed', 'Fri')
puts dayrange(0,'Tue',2,'Sat')
puts dayrange(4,2,5,1,3)