LoginSignup
0
0

More than 5 years have passed since last update.

rake taskで可変長引数を使う

Posted at

約1年間Railsエンジニアやってて初めてrake task書いた。

その時のめも。

to_a メソッドを使う

namespace :test_task do
  desc '可変長引数を受け取るtask'
  task :hoge, ['huga'] => :environment do |_task, args|
    p args.to_a
    p args.huga
  end
end

実行する。

$ bundle exec rake test_task:hoge[1,2,3,4]
["1", "2", "3", "4"]
"1"

argsHashではなくRake::TaskArgumentsであり、Rake::TaskArgumentsのインスタンスメソッドである
to_aを使うことで引数の値をすべて取得する事ができる

  • リファレンス

[http://www.rubydoc.info/gems/rake/12.0.0/Rake/TaskArguments#to_a-instance_method]

0
0
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
0
0