Implementing an Internal DSL in Ruby (simplified version)

chromatic on 2008-02-16T21:42:08

If you're like me, your attention span on a three-day weekend is too short to wade through all of Implementing an Internal DSL in Ruby. I've decided to be helpful to all of the confused people in the world who just don't see the special magic that goes in to writing an internal DSL. Here's the condensed version which helpfully fits on an index card. (That way, you can palm it at the next Rails meetup if you need a cheat sheet.)

  1. Create a class.
  2. Declare class methods in that class. These methods should take blocks as their only arguments. These methods should execute those blocks.
  3. Use the class in your programs.

If this doesn't sound very spectacular, you're absolutely right. I thought it important to take a break before the final, magical step which turns bog-standard rote programming into unicorn-and-glitter wonderful tooth-whitening DSL creation.

  • Pat yourself on the back vigorously, repeatedly, and noisily in full public view of everyone who might ever have a passing interest in computers, even if that interest is solely in the game Minesweeper.

Congratulations! Unlike the poor slobs who merely declare classes and methods, give them meaningful names, and occasionally pass around first-class functions, YU WRITED AN DSL!! GRATE JOBZES!!!


Crikey

pdcawley on 2008-02-17T08:49:40

i only skimmed that one first time past thinking "Well, it's a wee bit content light", but on second reading...

That took him months?

Frankly, I don't start to think it might be a pidgin until the code in those blocks starts looking pretty damned unrubylike. Class methods are for introducing the weird scope you need to make your declarative style pidgin work. Perl libraries like Jifty::DBI::Schema do something similar, they just have a different toolchain and 'feel' to them because Perl's prototyping pushes the style in that direction.