My question is similar to this one: How to detect if my shell script is running through a pipe?. The difference is that the script I’m working on is written in Ruby.
Let’s say I run:
./test.rb
I expect text on stdout with color, but
./test.rb | cat
I expect the color codes to be stripped out.
Use
$stdout.isatty
or more idiomatically,$stdout.tty?
. I created a little test.rb file to demonstrate, contents:Results:
Reference: https://ruby-doc.org/core/IO.html#method-i-isatty