Arguments in rake task

There are three ways to do this.

Method 1, environmental variables

desc "Example of a multi-argument raketask via environmental variables"
# rake with_environmentals name=Liene surname=Viktus
task with_environmentals: :environment do
  name = ENV['name']
  surname = ENV['surname']
  print name; print " "; print surname

Method 2, named argument hash

desc "Example of a multi-argument raketask via argument hash"
# rake with_hash  #=> John Smith
# rake with_hash[Chris,Brown] #=> Chris Brown
task :with_hash, [:name, :surname] => :environment do |t, hash|
  hash.with_defaults(name: :John, surname: :Smith)
  puts "Arguments with defaults were: #{hash}"
  print; print " "; print hash.surname

Bonus Method 3, anonymous argument hash

desc "Bring it on, parameters!"
# rake infinite_paramers['The','World','Is','Just','Awesome','Boomdeyada']
# via
task infinite_parameters: :environment do |task, args|
    puts args.extras.count
    args.extras.each do |params|
        puts params


Written on February 27, 2015