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

from
timocracy.com
:
```
```
require 'rake'

def capture_stdout
  s = StringIO.new
  oldstdout = $stdout
  $stdout = s
  yield
  s.string
ensure
  $stdout = oldstdout
end

Rake.application.rake_require 'metric_fetcher', ['../../lib/tasks']
results = capture_stdout {Rake.application['metric_fetcher'].invoke}
```
```