A collection of computer systems and programming tips that you may find useful.
Brought to you by Craic Computing LLC, a bioinformatics consulting company.

Monday, July 6, 2009

Passing Arguments to Rake with a Rails application

In order to pass arguments to a Rake task you specify them as named arguments in the task definition and then pass the actual values in square brackets following the rake task on the command line.

Here is a simple example:
task :mytask, [:word0, :word1] do |t, args|
puts "You typed: '#{args.word0}' and '#{args.word1}'"

You run this from the command line thus:
$ rake mytask[hello,world]
You typed: 'hello' and 'world'

Interacting with a Rails app involves adding a dependency of :environment. Add this after the array of arguments like this:
task :mytask, [:word0, :word1] => :environment do |t, args|
puts "You typed: '#{args.word0}' and '#{args.word1}'"
The syntax looks weird but it works.


1 comment:

House 9 said...

Thanks that was driving me crazy - where to put the => :environment

Archive of Tips