LoginSignup
6
11

More than 5 years have passed since last update.

railsのrakeタスクについて

Posted at

rakeとは

rubyで処理内容を書けるビルドツール。

rakeの設定方法

インストール

$ gem install rake

rakefile

rakefileにrakeが実行する処理内容を書く。下記の4項目が主。

  • 名前・・・タスクを識別するための名前。rakeコマンドで実行するタスクを指定する場合などに使用します。
  • アクション・・・タスクで行われる一連の処理です。省略可です。
  • 事前タスク・・・前提として実行するタスクの一覧です。指定された事前タスクは、タスクの実行前に実行されていなければ実行されます。
  • パラメータ・・・タスクに渡されるパラメータ名です。rakeコマンドの引数で
task タスク名 do
処理内容
end

# "hello" を表示するだけのタスク
task :hello do
  puts 'hello'
end

タスクをグループ化するために名前空間でくくる

# ネームスペースの中にタスクを定義する。
namespace :test do
  task :hello do
    puts 'hello'
  end
  task :hello_world=>[:hello] do
    puts 'world'
  end
end

rakeを定期的に実行する方法

rakeコマンド

6
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
11