On 12/14/07, Ruby Quiz <james / grayproductions.net> wrote:

>         The Game
>
> Eric Hodel described Programmer Ping-Pong in his RubyConf 2007 presentation.  I
> wasn't familiar with the concept before that and it sounds like fun, so let's
> all try it out together.
>
> The rules are:
>
>         * This quiz does not have a no-spoiler period so you may
>           submit at anytime after reading this message
>         * 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're doing this in the true tdd spirit of making very small steps right.

Okay, first return.

avl_tree.rb
#!/usr/bin/env ruby -wKU

class AVLTree

  def empty?
    !@contents
  end

  def include?(obj)
    return @contents == obj
  end

  def <<(obj)
    @contents = obj
  end

end

test_avl_tree.rb
#!/usr/bin/env ruby -wKU

require "test/unit"

require "avl_tree"

class TestAVLTree < Test::Unit::TestCase
  def setup
    @tree = AVLTree.new
  end

  def test_tree_membership
    assert_equal(true,  @tree.empty?)
    assert_equal(false, @tree.include?(3))

    @tree << 3

    assert_equal(false, @tree.empty?)
    assert_equal(true,  @tree.include?(3))
  end

  def test_tree_should_allow_more_than_one_element
    @tree << 3
    @tree << 4

    assert(@tree.include?(4))
    assert(@tree.include?(3))
  end

end
-- 
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/