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]
  end

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]
end

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]
end

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

http://vivifystudios.com/articles-css-inline-block/

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: https://github.com/cwninja/geminabox

The instructions:

#install the gem

gem install geminabox

# create config.ru file

mkdir gems; cd gems

vim config.ru

#config.ru content

require "rubygems"

require "geminabox"

Geminabox.data = "/var/geminabox-data" # …or wherever

run Geminabox

#save

# create gems directory

mkdir -p /var/geminabox-data/gems

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

# run the server

rackup config.ru

# That's it J

Go to http://127.0.0.1:9292 to see your own gems server

2 Comments

Delicious JSON and RSS APIs

http://www.delicious.com/help/json

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:

https://github.com/gregorymostizky/nutritious

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: http://rubybenchmark.com/

Just another awesome idea

Leave a comment

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

http://unixwiz.net/ndos-shame.html

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

cc_number.gsub!(/[-\s]/,”)

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

Leave a comment

Awesome iPhone style zooming for any jQuery-enabled webpage

http://janne.aukia.com/zoomooz/

The demo is a must see

Leave a comment

Follow

Get every new post delivered to your Inbox.