ruby-on-railsrails-activejobsplat

Why do rails jobs (ApplicationJob) use splat operator?


I've created my first job using the rails generator:

rails g job do_something

which created the job file:

class DoSomethingJob < ApplicationJob
  queue_as :default

  def perform(*args)
    # Do something later
  end
end

The splat operator is new to me (I understand that it can be used to pass multiple arguments and it will group them into an array). My question is, why do jobs come with the splat operator by default - def perform(*args)? My temptation is to simply delete the default *args but I'm worried I might be missing something.

Is there anything wrong with deleting the default *args and creating arguments like I would for any other method? Or do I need to be using the splat operator with all jobs?

Any and all help appreciated!


Solution

  • The construction of the perform method and its use depends exclusively on you; if you remove the splat operator, you won't miss a thing, invoking a job doesn't pass implicit parameters or anything you should handle with args.last or something like that.

    Anyway, the splat operator only offers you the same versatility as in any ruby ​​method, and rails use it like a placeholder.