Ruby in Action

Hans Fugal hans at fugal.net
Thu Feb 15 14:15:36 MST 2007

```A biologist friend came to me and had some questions. She knows C and is
learning C++. Her questions were many, but it boiled down to "help, I've
fallen and I can't get up".

Her advisor advised her not to take classes on C++ or computer science,
but to learn by doing projects. He handed her a learning project to
convert a text representation of a bifurcating tree into a picture. He
said, use the factory pattern and a stack and the Boost graph library.
You can see where this is going. I helped her get a handle on tree data
structures, how they relate to graphs (so she could use boost graph
library if she wanted), and showed her how to use the STL stack and
talked a little about object oriented programming. It was a good
session, but it left her still overwhelmed at the amazing amount of work
it would be just to parse a string representation of a tree into a graph
structure (and I didn't have the heart to tell her about all the
backflips she'd probably have to do to get the boost graph library to
actually output something interesting).

Then I said, "Here's how I would do it in Ruby." We sat down at my
laptop and I whipped this out:

# sample input: "((A,B),C)"
puts "graph {"

v = 0 # current vertex
\$stdin.each_byte { |b|
b = b.chr # ASCII to string
case b
when '('
if v == 0 # root node has no parents
p = v = 1
else
p = v # parent
v = 2*p # left child
puts "#{p}--#{v}"
end
puts "#{v} [shape=point]"
when /[a-zA-Z]+/
puts "#{v}--#{b}"
puts "#{b} [shape=plaintext]"
when ','
when ')'
v = v/2
end
}

puts "}"

Yes, it needs to be modified to store a tree data structure for the real
work to come, but I was being agile and just writing the simple code to
tackle the task at hand.  Several lessons inherent here. First, agile
programming. Second, the evil of monstrosities like the boost graph
library. Third, the evil of C++. Fourth, the evil of trying to do in
code what can be done with the use of small specialized tools (like
graphviz in this case). Fifth, liberal parsing is often just fine (I
guess that's part of agility)

Thought you might enjoy. She did. She's going to try and convince her
advisor to let her use ruby (or perl or python or something not C++).
Unfortunately I don't think she'll be very successful. If he's a non-CS
type using libraries like Boost and the Factory pattern where it's not
needed, he's probably a self-taught software "engineer" who won't
believe things can or should be simple.

--
Hans Fugal ; http://hans.fugal.net

There's nothing remarkable about it. All one has to do is hit the
right keys at the right time and the instrument plays itself.
-- Johann Sebastian Bach

```