Reputation: 31
It's not clear to me how "puts" works in Ruby for the output of a command that throws its output to stderr.
Look at this code:
command="/usr/bin/java -version"
result=`#{command}`
puts result
puts "XX#{result}XX"
The result is:
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)
XXXX
java -version sends its output to stderr (I am aware that in order to prevent this I should have used 2>&1 at the end of the command)
My question: The variable "result" in reality is empty, but the first puts prints the stderr output and the second puts the stdout (which is empty). Why? What's going on?
Upvotes: 1
Views: 500
Reputation: 2699
Actually, both puts
statements print nothing. If you do this:
puts "YY#{result}YY"
puts "XX#{result}XX"
You'll see that you get YYYY
and XXXX
.
The reason that you see the output from Java is because Java itself is printing to stderr which isn't being captured by Ruby.
Upvotes: 2
Reputation: 368944
The first puts result
prints only newline. The output come from the following line (by the command itself, printing to stderr)
result=`#{command}`
You can confirm that by removing all puts ...
lines.
Upvotes: 0