rakeタスクから何らかの値を返す方法
概要
Ruby で Rake タスクから値を返すにはどうすればよいですか。
サンプルコード:
namespace tasks
task task1: :environment do |task|
log = "Running task"
puts log
log << "Done"
return log # suggest how to do this
end
end
Rake タスクを Rake::Task[‘tasks:task1’].invoke として実行しています。次のように変数の戻り値を取得するにはどうすればよいですか。
result = Rake::Task['tasks:task1'].invoke
解決策
タスクの結果を他のタスクに含めたいと仮定します。
config = ''
task :load_config do
config = 'some config' # this could be reading from a file or API
end
# this tasks depends on the other
task use_config: [:load_config] do
puts config
end
それから:
$ bundle exec rake use_config
some config