The three rules of Ruby Q.:
Please do not post any solutions or spoiler discussion for this quiz
48 hours have passed from the time on this message.
Support Ruby Q. by submitting ideas as often as you can:
Suggestion: A [QUIZ] in the subject of emails about the problem helps
on Ruby T. follow the discussion. Please reply to the original quiz
if you can.
This is a non-traditional Ruby Q. that changes the rules of the
Please read the entire message before playing along. We will be back to
quizzes next time, for those that end up missing them.
Eric H. described Programmer Ping-Pong in his RubyConf 2007
wasn’t familiar with the concept before that and it sounds like fun, so
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
I want to see if we can build an entire library using just that process.
Let’s build a pure Ruby binary AVL tree. An AVL tree is a
tree data structure where insertion, deletion, and lookup all take O(log
to execute. This is handy to have for many search problems that must
quickly. It can also be used to build constructs like an OrderedHash.
You can read more about AVL trees on Wikipedia:
There’s also a handy document describing their rotations in detail:
What features our AVL tree will support will be decided by you as you
tests. Here are some suggestions of things we might try though, just to
- Support for custom Ruby objects as nodes of the tree
- The ability to customize the comparison code, perhaps with a block
- A String output visualizer, possibly for the inspect() method
- Any other great features you can think of to add
We will have two files: avl_tree.rb and test_avl_tree.rb. Please pass
files in each submission email you make for this quiz. Let’s not
this with directory structures or zip files.
Please don’t add any external dependencies, unless it’s a standard
want everyone to be able to easily run this code and play along.
We are using Test::Unit instead of RSpec, or any other tool, for similar
Please keep your tests short. Under 10 lines is preferred, but don’t go
Also try to test just one aspect of the implementation with each test.
purposely say “aspect” and not “method.” I do test more than one method
serve and I can imagine other scenarios where it could be useful, like
support for a handful of the standard Enumerator methods.
You can refactor any code as needed provided you do not change its
all tests still pass after you do so.
Adds comments if you need to, but writing code that needs no comment
Let’s use some simple spacing conventions to keep all of us on the same
Indent two space and do not use tabs. Break up long lines so they do
Finally, this quiz has the potential to be very chaotic. Take pity on
quizmaster who must track this process and on the rest of the community
be bothered by a highly active thread. I suggest good email manners:
- 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
The initial contents of avl_tree.rb are:
#!/usr/bin/env ruby -wKU
The test file, test_avl_tree.rb, begins as:
#!/usr/bin/env ruby -wKU
class TestAVLTree < Test::Unit::TestCase
@tree = AVLTree.new
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