Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass multiple parameters to rake task

Tags:

ruby

rake

I want to pass multiple parameter but I don't know the numbers. Such as model names. How do I pass those parameters into a rake task and how do I access those parameters inside the rake task.

Like, $ rake test_rake_task[par1, par2, par3]

like image 595
krunal shah Avatar asked Aug 27 '10 18:08

krunal shah


People also ask

How do I run a rake task?

Go to Websites & Domains and click Ruby. After gems installation you can try to run a Rake task by clicking Run rake task. In the opened dialog, you can provide some parameters and click OK - this will be equivalent to running the rake utility with the specified parameters in the command line.


2 Answers

You can use args.extras to iterate over all the arguments without explicitly stating how many parameters you have.

Example:

desc "Bring it on, parameters!" task :infinite_parameters do |task, args|      puts args.extras.count     args.extras.each do |params|         puts params     end          end 

To run:

rake infinite_parameters['The','World','Is','Just','Awesome','Boomdeyada'] 

Output:

6 The World Is Just Awesome Boomdeyada 
like image 164
Ardee Aram Avatar answered Sep 28 '22 06:09

Ardee Aram


Rake supports passing parameters directly to a task using an array, without using the ENV hack.

Define your task like this:

task :my_task, [:first_param, :second_param] do |t, args|   puts args[:first_param]   puts args[:second_param] end 

And call it like this:

rake my_task[Hello,World] => Hello    World 

This article by Patrick Reagan on the Viget blog explains it nicely

like image 41
Henry Collingridge Avatar answered Sep 28 '22 06:09

Henry Collingridge