Ruby Tree exercise (day 2) - how to best TDD

26 views
Skip to first unread message

Pierre M

unread,
May 6, 2013, 10:12:28 AM5/6/13
to london-c...@googlegroups.com
Hi all,
for those who did, do or will do the Tree exercise of Ruby's day 2 (in
the 7 Languagues book). The aim is to build a Tree from a Hash.

First of all, please don't hesitate to tell me if this kind of post is
not appropriate for this mailing list.

~ ~ ~ ~ ~

What made this exercise interesting for me, was starting without a
function to test that two Trees are equal. How do i check that i have
built the right Tree, then?

What i did (it's probably not the best solution), was to build both
the test (that two Trees are equal) and the actual Tree-building
functions at the same time. I used the fact that we already have a
Tree constructor, to improve the test function incrementally. And then
created the corresponding Hash-Tree translation. I did this in small
increments.

I started with an empty Tree. Then I proceeded in increments where i would
- first, check that my new Tree (one node, for example - built
with the existing constructor) was not equal to the previous one.
=> this made me improve the test function.
- then, check that a one-node Tree created with the Hash-Tree
translator, was what i expected (comparing the result with the output
of the existing Tree constructor)
=> this made me improve the Hash-Tree translation. But, also sometimes
i had to fiddle with the test function, which didn't always feel
right.

It worked, but i found myself thinking: "there must be a better way".
I think that a good alternative is to first, create and test a
function that turns a Tree into a Hash. Then, you can use existing
tools to compare two Hashes, which gives you a straightforward
Tree-comparison test.
I don't have the time to do this exercise again this week, though.

- Do you think this approach will be better?

- Has anyone found another interesting approach?

I've wondered if there could be a way to "visit" two Trees in
parallel, to compare them. But apparently it's not possible to pass
two parameters to a block, so that seems impossible with the
implementation of "visit".
Maybe by passing an actual function instead of a block?
Reply all
Reply to author
Forward
0 new messages