Ruby 中 $STDOUT 与 STDOUT 的区别

在 Ruby 中,$stdout(前面有一个美元符号)和 STDOUT(全部大写)之间的区别是什么?在执行输出重定向时,应该使用哪个选项以及为什么?$stderrSTDERR也是如此。

编辑: 刚刚发现一个 相关问题

40866 次浏览

$stdout is a global variable that represents the current standard output. STDOUT is a constant representing standard output and is typically the default value of $stdout.

With STDOUT being a constant, you shouldn't re-define it, however, you can re-define $stdout without errors/warnings (re-defining STDOUT will raise a warning). for example, you can do:

$stdout = STDERR

Same goes for $stderr and STDERR


So, to answer the other part of your question, use the global variables to redirect output, not the constants. Just be careful to change it back further on in your code, re-defining global variables can impact other parts of your application.

  • STDOUT is a global constant, so it should not be changed.
  • $stdout is a predefined variable, so it can be changed.

If you are using the shell to do redirection:

$ ruby test.rb > test.log

then it doesn't matter which one you use as the file descriptor for your script is being determined before your script is executed.

However, if you are trying to change the file descriptor for the OS's STDOUT from within your Ruby script, for example to send output to a rotating set of log files based on the current day of the week, then you'll want to make sure you use $stdout.

Both $stdout and STDOUT have different meanings. Ruby's documentation is pretty clear on this topic:

  • $stdout – The current standard output.
  • STDOUT – The standard output. The default value for $stdout.

When you want to write to the standard output, then you actually mean the current standard output, thus you should write to $stdout.

STDOUT isn't useless too. It stores the default value for $stdout. If you ever reassign $stdout, then you can restore it to the previous value with $stdout = STDOUT.

Furthermore, there's one more predefined variable:

  • $> – The default output for print, printf, which is $stdout by default.

However it looks like in Ruby 2.3 it simply behaves as an alias for $stdout. Reassigning $stdout changes the value of $> and vice versa.