How can I pass an array as an argument to a Rake task?

One of the soulutions is to avoid , symbol in the string, so your command line would look like:

$ rake change_statuses['1 2 3', foo, bar]

Then you can simply split the IDs:

# Rakefile

task :change_statuses, :ids, :current_status, :new_status do |task, args|
  ids = args[:ids].split ' '

  puts "args were #{args.inspect}"
  puts "ids were #{ids.inspect}"
end

Or you can parse the ids value to get your expected output:

args[:ids] = args[:ids].split(' ').map{ |s| s.to_i }

I’m using rake 0.8.7

Leave a Comment