Rake task with variable arguments

G

Glenn Ritz

Hi,

I want to create a Rails rake task that can accept a varying number of
arguments. Ideally, I'd like the arguments to be optional, and I'd also
like to be able to specify 1 or more.

So, if I have the rake task my_task, like this:

task :do_something, :arg1 do |t, args|
puts args.inspect
# some code goes here
end

I's like to be able to execute this like this:

rake do_something

rake do_something[1]

rake do_something[1, 2, 4]

I'd like args to be an empty array in the first call, a one element
array in the second, and a 3 element array in the third. I think 0
arguments or 1 is simple -- the way I've written the task above does
this much. But I don't know how to get it to accept more than 1. Is
there a way to do this?

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top