tags:

views:

1577

answers:

2

I've got a Rakefile with a rake task that I would normally call from the commandline (rake blog:post Title).

I'd like to write a ruby script that calls that rake task multiple times, but the only solution I see is shelling out (`` or system).

What's the right way to do this?

+9  A: 

from timocracy.com:

require 'rake'
require 'rake/rdoctask'
require 'rake/testtask'
require 'tasks/rails'

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

Rake.application.rake_require '../../lib/tasks/metric_fetcher'
results = capture_stdout {Rake.application['metric_fetcher'].invoke}
Titanous
A: 

Works like a charm. Now to fix my rake task :).

mando