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]
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With