My first ruby script is coming along nicely

Among other things, thanks to the nice folks at #ruby-lang.

Ruby has some really nice schticks. A few of the things I currently love about ruby:

  • The String class has some really useful methods (unlike some other langauge’s String class which is anoying). For example, the subscript operator [] has some very interesting capabilities – not only it takes all kinds of indexes and ranges, it can also take another String and do a strstr(), or – more useful to me – it can take a regular-expression and return what that that regexp matched.
  • the case statement in ruby returns a value, and that value can be immidietly used as the reciver of a method call, making the following code useful;

    case val.class
    when Array then val.flatten
    when Hash then val.values
    else val.to_a
    end.each { |g| puts g}

    Or some such

Anyway – I’m about half way through my comics script port to Ruby (from Python, after previously it was in Perl) and while most of the previous work has been to learn how to work with Ruby and SQLite (which I chose as my storage backend for this implementation), I’m now starting to get the hang of it.

One more thing of note:
My implementation strategy is to use a command dispatcher to access all the capabilities of the script. This can be done ingenisouly clean – The base command class Action is the command factory and dispatcher – it offers a Action#dispatch(args). It then extracts the first argument and matches it to the list of available Action implementations and executes the required one, passing it the rest of the arguments.
The ingenious part is that I don’t have to actually maintain the command registery apart of the actuall implementaion! Action is implementing a inherited(class) method which gets called whenever a new class is defined that extends the Action base class. inherited then maintains the command registery automatically – in order to add a new command I just have to implement it. Additionally, due to the object oriented nature, command sub classes can implement their own dispatch mechanism just by called the Action#dispatch – they don’t even have to manager their own registery as this is also done automatically by Action#inherited!

Tre cool.

Leave a Reply