Home > Software engineering >  Why we use the t as an arguments in rake task
Why we use the t as an arguments in rake task

Time:07-24

namespace :git do
  desc "let's you clone a git repo by passing username and repo"
  task :clone, :user, :repo do |t, args|
    user = args[:user]
    repo = args[:repo]
    if system "git clone [email protected]:#{user}/#{repo}.git"
      puts "Repository: #{repo} cloned successfully"
    else
      puts "There was a problem with your request, please try again later."
    end
  end
end 

Can anyone explain why we take t as an argument here and the use of t in this task.

CodePudding user response:

The first argument of the task definition block is the task object itself, an instance of the Rake::Task class.

If you ask "why do I need it" - you probably don't need it (yet?), but in general, it gives you an access to the task's state and instance methods, and therefore more control on how the task is executed.

For example, by default Rake engine tracks the tasks that were executed and don't execute them on consequent invocation attempts in the same chain, but having access to the task object you can call reenable on it, making its invocation possible again. Or you can enhance the task with some prerequisites dynamically etc etc etc.

CodePudding user response:

Task gets a block:

  • The first argument of the block “t” is the current task object.
  • The second argument “args” allows access to the task arguments.

See rake rdoc

The first argument t is not used at all in your example.

  • Related