How do you declare a Rake task that depends on a parameterized task?

Args are passed down through the call stack. You just need to make sure your top-level task captures all the arguments all of the dependencies require. In your case you’ll want to put first_name and last_name on the send_letter task.

Here is an example that shows named arguments defined elsewhere flowing into the dependency (even if they aren’t defined in the dependency), but the argument that doesn’t match the name of the top-level task argument is nil.

desc 'Bar'
task :bar, :nom do |task, args|
  puts "BAR NOM: #{args[:nom]}"
  puts "BAR NAME: #{args[:name]}"
end

desc 'Foo'
task :foo, [:name] => :bar do |task, args|
  puts "FOO NAME: #{args[:name]}"
end

Running rake foo[baz] yields

BAR NOM: 
BAR NAME: baz
FOO NAME: baz

It is interesting to note that using args.with_defaults(nom: 'Jaques') in the foo task has no effect on the dependent task — nom is still nil.

Rake version: rake, version 10.0.3

Ruby version: ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-darwin11.3.0]

Leave a Comment