- Add the
rake
gem to yourGemfile
:
gem 'rake'
-
Install it by running
bundle install
. -
Create a
Rakefile
containing:
# frozen_string_literal: true
Rake.add_rakelib(File.join('lib', 'tasks'))
- Create a
hello.rake
file in./lib/tasks
containing your code:
# frozen_string_literal: true
# Require your code here:
# require_relative '../../config/environment'
namespace :hello do
task :world do
puts 'Hello World!'
end
task chain: :world do
puts 'Chain!'
end
task :name, [:who] do |_task, args|
puts "Hello #{args[:who]}!"
end
end
- Run it:
bundle exec rake hello:world
# Hello World!
bundle exec rake hello:chain
# Hello World!
# Chain!
bundle exec rake hello:name[Rake]
# Hello Rake!