Running Rake with Parameters

I’ve recently discovered a little know feature of Rake that I would like to share.

Everyone who ever used Rails knows about rake – stuff like rake db:migrate has become automatic for a lot of us. A lot of people have also written their own rake tasks. While default Rails tasks don’t take parameters you sometimes want to make a task that has them.

The usual way to do it is to use environment variables – something like this:

  rake db:migrate ENV=production

And then you can access those parameters in the task like this:

  desc 'my task'
  task :my_task => :environment do 
    puts ENV[:ENV]

While this works, it feels a bit hacky – environment variables were not meant to be used like that.
Turns out there is a cleaner solution.

desc 'parametrized task'
task :ptask, [:param1, :param2] => [:environment] do |t, params|
  puts params[:param1]
  puts params[:param2]

What happens here is that we used a special rake syntax that defines parameters for the task – that’s what the first array is about. The second array is for the dependencies.

To invoke this task you do:

rake ptask[1,2]

The extra nice thing about this approach is that the parameters become part of task declaration. If you run rake -T you will see the task listed with all its parameters, so you don’t have to remember them in your head. Another cool thing is that the parameters defined in this way propagate through dependencies. For instance:

desc 'another task'
task :atask, [:param1, :param2] => [:ptask, :environment] do |t, params|
  puts params[:param1]
  puts params[:param2]

If you now call: rake atask[1,2] both the atask and the ptask will get the parameters as needed.


Leave a comment

CSS layout tricks simply explained in demoed

Leave a comment

Hosting your own rubygems compatible server in 3 easy steps

Amazing little project that allows you to create and host your own gems:

The instructions:

#install the gem

gem install geminabox

# create file

mkdir gems; cd gems

vim content

require "rubygems"

require "geminabox" = "/var/geminabox-data" # …or wherever

run Geminabox


# create gems directory

mkdir -p /var/geminabox-data/gems

chown 777 /var/geminabox-data/gems -R

# run the server


# That's it J

Go to to see your own gems server


Delicious JSON and RSS APIs

Allows "streaming" new and popular bookmarks, as well getting only bookmarks by specific tags.

I’ve made a simple gem for loading recent bookmarks and most popular tags for them, so if you are interested in an example of how to use those feeds you can at my code at:

Leave a comment

Very nice idea – microbenchmarking site & gem for ruby

Makes creating ruby micro benchmarks easier than ever.

And once you are done, it’s super easy to share the benchmark with the community:

$ gem install bench_press
$ bench_press --publish my_benchmark.rb

The website is here:

Just another awesome idea

Leave a comment

Hall Of Shame – websites that can’t handle dashes/spaces in credit card numbers

How lazy you need to be to not include this simple line of code?


The error handling and error messages take more space than that…

Leave a comment

Awesome iPhone style zooming for any jQuery-enabled webpage

The demo is a must see

Leave a comment