Basic Metaprogramming with Ruby and Rake


I needed a script to control a batch workflow process. There were a lot of moving parts to the process and dividing it into mutliple scripts or using command line arguments did not seem feasible. Rake looked like a perfect fit for this problem. One particular sub-problem, was I needed to process several files in a particular way. Originally, I thought I only needed to process them all at once, so I wrote a rake task like the following:

First Iteration

Second Iteration

However, in order to debug the process, I needed to process the files one by one. At first I started to create seperate tasks for each file. But then I remembered that there is nothing special about the way tasks are defined, and I can define tasks using some basic metaprogramming:


Really good DSLs like Rake and Sinatra make me forget I am writing Ruby. While I feel restricted (in a good way) to this subset of commands, at any time I can use the full power of Ruby if I need it. Being able to dynamically create tasks let me quickly create a script that is now up to ~40 tasks (and growing).

While this is starting to get a little bit unweidly, the equivalent bash script would be much worse. What keeps the script manageable is that much of the process is specificied as data. Most of the program is processing a hash table and creating a task per entry the hash table.