Chip's Quips
A tiny spark of wit for a highly flammable world

Tapping the Heels of my Ruby Slippers

January 19th, 2006 7:09:00 am pst by Sterling Camden

Did I mention that I’m jazzed about the Ruby programming language? Oh yeah, over here. Well, in order to test out the capabilities of the language, I decided to write an expression processor. It’s posted on chipstips.com.

Of all of the expression processors I have written over the years, this one definitely packs the highest functionality:code ratio. It can be made to evaluate many different types of expression. Operators and functions can all be defined by the client program, which also manages associations between identifiers and their values via a code block. It handles algebraic, Polish, Reverse Polish, or mixed operand/operator order.

All of this was accomplished with only 260 lines of code, including mix-in modules to easily enable support for the “standard” sets of operators (grouping, assignment, arithmetic, Boolean, and last result). Perhaps some Ruby gurus can improve on this or reduce its size, but I’m pretty happy with the result. It showcases several of Ruby’s unique features, such as code blocks, mix-in’s, reflection and dynamic method invocation.

I realize that in the sample client application I could have simply extended the ExpressionProcessor class with the included modules, rather than deriving a new class first. That’s fine so long as you know your code is the only place in the application that will be using this class, but personally it rubs me the wrong way. In an enterprise application, that seems like asking for trouble. I can see extending existing classes for things like debugging and instrumentation, though.

Ruby has given me a new outlook on programming languages. Somehow, Toto, I don’t think we’re in Kansas anymore. Next, I’ll investigate Ruby on Rails to see whether that’s a Good Witch or a Bad Witch.

Posted in Coding...OK? | No Comments » RSS 2.0 | Sphere it!

Leave a Reply

Better Tag Cloud