问题
In a ruby file:
When I do system("rspec file_spec.rb") I get a nice colored output.
When I do this:
result = `rspec file_spec.rb`
puts result
I get no color at all.
Is there any way to preserve the color?
btw, I'm on OSX with Terminal.app if that matters.
回答1:
From reading the code it looks like RSpec calls IO#isatty on the output stream to decide whether or not to colorize the output. The backquote method must work differently from system in this respect.
EDIT
This works if you add the option --tty to the rspec command:
`rspec --color --tty file_spec.rb`
as mentioned in this SO question.
来源:https://stackoverflow.com/questions/5078950/how-do-i-get-the-color-from-the-backticks-command-in-ruby