<p>I have a <code>Rakefile</code> with a Rake task that I would normally call from the command line:</p>

<pre><code>rake blog:post Title
</code></pre>

<p>I'd like to write a Ruby script that calls that Rake task multiple times, but the only solution I see is shelling out using `` (backticks) or <code>system</code>.</p>

<p>What's the right way to do this?</p>

[{'answer_id': 3543, 'author': 'titanous', 'author_id': 399, 'author_profile': 'https://Stackoverflow.com/users/399', 'pm_score': 7, 'selected': True, 'text': '<p>from <a href="http://web.archive.org/web/20090412040557/http://timocracy.com:80/articles/2008/02/21/calling-invoking-rails-rake-tasks-from-within-ruby-for-testing-try-2" rel="nofollow noreferrer">timocracy.com</a>:</p>\n\n<pre><code>require \'rake\'\n\ndef capture_stdout\n  s = StringIO.new\n  oldstdout = $stdout\n  $stdout = s\n  yield\n  s.string\nensure\n  $stdout = oldstdout\nend\n\nRake.application.rake_require \'metric_fetcher\', [\'../../lib/tasks\']\nresults = capture_stdout {Rake.application[\'metric_fetcher\'].invoke}\n</code></pre>\n'}, {'answer_id': 11520262, 'author': 'knut', 'author_id': 676874, 'author_profile': 'https://Stackoverflow.com/users/676874', 'pm_score': 4, 'selected': False, 'text': '<p>You can use <code>invoke</code> and <code>reenable</code> to execute the task a second time.</p>\n\n<p>Your example call <code>rake blog:post Title</code> seems to have a parameter. This parameter can be used as a parameter in <code>invoke</code>:</p>\n\n<p>Example:</p>\n\n<pre><code>require \'rake\'\ntask \'mytask\', :title do |tsk, args|\n  p "called #{tsk} (#{args[:title]})"\nend\n\n\n\nRake.application[\'mytask\'].invoke(\'one\')\nRake.application[\'mytask\'].reenable\nRake.application[\'mytask\'].invoke(\'two\')\n</code></pre>\n\n<p>Please replace <code>mytask</code> with <code>blog:post</code> and instead the task definition you can <code>require</code> your rakefile.</p>\n\n<p>This solution will write the result to stdout - but you did not mention, that you want to suppress output.</p>\n\n<hr>\n\n<p>Interesting experiment:</p>\n\n<p>You can call the <code>reenable</code> also inside the task definition. This allows a task to reenable himself.</p>\n\n<p>Example:</p>\n\n<pre><code>require \'rake\'\ntask \'mytask\', :title do |tsk, args|\n  p "called #{tsk} (#{args[:title]})"\n  tsk.reenable  #&lt;-- HERE\nend\n\nRake.application[\'mytask\'].invoke(\'one\')\nRake.application[\'mytask\'].invoke(\'two\')\n</code></pre>\n\n<p>The result (tested with rake 10.4.2):</p>\n\n<pre><code>"called mytask (one)"\n"called mytask (two)"\n</code></pre>\n'}, {'answer_id': 15259172, 'author': 'Kelvin', 'author_id': 498594, 'author_profile': 'https://Stackoverflow.com/users/498594', 'pm_score': 5, 'selected': False, 'text': "<p>This works with Rake version 10.0.3:</p>\n\n<pre><code>require 'rake'\napp = Rake.application\napp.init\n# do this as many times as needed\napp.add_import 'some/other/file.rake'\n# this loads the Rakefile and other imports\napp.load_rakefile\n\napp['sometask'].invoke\n</code></pre>\n\n<p>As knut said, use <code>reenable</code> if you want to invoke multiple times. </p>\n"}, {'answer_id': 41839883, 'author': 'Dorian', 'author_id': 407213, 'author_profile': 'https://Stackoverflow.com/users/407213', 'pm_score': 2, 'selected': False, 'text': "<p>In a script with Rails loaded (e.g. <code>rails runner script.rb</code>)</p>\n\n<pre><code>def rake(*tasks)\n  tasks.each do |task|\n    Rake.application[task].tap(&amp;:invoke).tap(&amp;:reenable)\n  end\nend\n\nrake('db:migrate', 'cache:clear', 'cache:warmup')\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/3530', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1388162/']