Monday, May 21, 2007

How To Make Everything Look Like Python

On the ruby-talk list, someone posted recently that they wanted to use Python indentation in Ruby. It's probably a bit unhealthy to like Python that much.

But the question is an interesting one, so:

Here's Ruby code which allows you to write Ruby as if it were Python. You save your Python/Ruby in a .pyrb file and load it through pyruby.rb, which will then turn it into usable Ruby.

Here's a Perl library which allows you to do the same thing in Perl - but without the .pypl step. Perl has a feature called source filtering, which allows you to alter Perl's syntax from within Perl. Perl's army of wizards have already churned out a ton of filter libraries, which means that the Perl library which allows you to write Perl as if it were Python only takes about 200 lines of code.

The Perl community's starting to look more and more like the Lisp community every day. The combination of incredible power, reclusive wizards, and antisocial Slashdotters gives it the vibe of a lava-filled wasteland dotted with towers where strange men with white beards obsess over unspeakable knowledge. I spoke to someone once who compared programming in Lisp to studying Kabbalah, in that it does strange things to your head. Parts of Perl are like that. Still, source filtering's kind of cool. Unnecessary, but cool.

Update! Ruby version massively improved.


  1. If you haven't yet seen the perl entry for 99 bottles, go there now - it opens up a whole new appreciation of the sheer breadth and depth of geekness that's out there.

    Jaime Metcher

  2. Thanks for linking to my post on Python as Ruby. You got me thinking when you mentioned the Perl source filtering trick. After a bit of poking around I found you can sort of do that with Ruby.

    I've updated the post to include the new changes. It also allows deeper dedenting than previously to more mimic Python capabilities.

    Brad Phelan

  3. Wow, that's awesome! That Lisper's trick should theoretically work for nearly any language, too!

    The 99 bottles is cool too, but kind of frightening at the same time.


Note: Only a member of this blog may post a comment.