[QUIZ] Programmer Ping-Pong (#150)

(forwarding to Ruby Talk so all can benefit from this code)

···

Begin forwarded message:

From: Rob Biedenharn <Rob@AgileConsultingLLC.com>
Date: December 14, 2007 10:21:01 AM CST
To: Ruby Quiz <james@grayproductions.net>
Subject: Re: [QUIZ] Programmer Ping-Pong (#150)

On Dec 14, 2007, at 8:13 AM, Ruby Quiz wrote:

  * I'll make the initial serve, starting the quiz off with
    a single failing test
  * Anyone can return the ball at anytime by doing exactly
    two things, in order: make all tests pass including the
    recently added failure and then add a new failing test
    of your own

We will have two files: avl_tree.rb and test_avl_tree.rb. Please pass both
files in each submission email you make for this quiz. Let's not complicate
this with directory structures or zip files.

  * Use your client's "Reply" feature and make sure you are replying to
    the message that contains the test you made pass
  * Trim any unneeded context from the reply, including the previous
    version of the code since you will be including the current copy
    of the whole thing
  * Kindness to your fellow programmers trumps any listed guidelines

In the spirit of helping keep this sane (and because I'm interested in this quiz!), here are a couple small scripts that could help keep the posting/tracking of code civil. You pass the contents of an email to extract as STDIN and it will create the contained files. Running package gives you the files in a pastable format on STDOUT. By default, it gets the '*.rb' files, but I used: `./package '{package,extract}'` to generate what I pasted here.

-Rob

==> extract <==
#!/usr/bin/env ruby -wKU
# -*- ruby -*-

file = nil
state = :init
ARGF.each do |line|
case state
when :init
   next unless line =~ /^==> (.*) <==$/
   if File.exist?($1)
     backup = $1+'~'
     File.delete(backup) if File.exist?(backup)
     File.rename($1, backup)
   end
   file = File.open($1, 'w')
   state = :writing
when :writing
   file.write line
   if line.chomp == '__END__'
     file.close
     state = :init
   end
end
end
__END__

==> package <==
#!/usr/bin/env ruby -wKU
# -*- ruby -*-

Dir[ARGV[0] || '*.rb'].each do |f|
lines = IO.readlines(f)
lines.unshift "==> #{f} <==\n"
lines << "__END__\n" unless lines.last.chomp == '__END__'
lines << "\n"
puts lines
end
__END__

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
+1 513-295-4739
Skype: rob.biedenharn