Craig Walker Craig Walker - 1 month ago 11
Ruby Question

How can I preserve output color when executing a process in Ruby?

I'm using a helper script to execute

rspec
tests.

command = "rake spec #{path} #{scope}"
output = `#{command}`
puts output


This works fine, except that I lose all the colors from the
rake rspec
output. The appropriate ANSI codes do not appear to be contained within the
output
string.

How can I execute a process so that it returns output which includes the text color?

Answer Source

Kernel.exec() gets me the solution I want (colored rspec output), but it does so by replacing my ruby script process with the rspec process. That means I can't do anything with the output or run anything after the rspec call.

That's acceptable in my particular situation, but less than ideal as a general solution. So I'd like a better answer if available.