In Ruby, what is the difference between $stdout
(preceded by a dollar sign) and STDOUT
(in all caps)? When doing output redirection, which should be used and why? The same goes for $stderr
and STDERR
.
在Ruby中,$stdout(前面有一个美元符号)和stdout(所有大写)有什么区别?在输出重定向时,应该使用哪个,为什么?同样的道理也适用于$stderr和stderr。
Edit: Just found a related question.
编辑:刚刚找到一个相关的问题。
3 个解决方案
#1
93
$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
.
$stdout是表示当前标准输出的全局变量。STDOUT是一个表示标准输出的常量,通常是$ 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是一个常量,您不应该重新定义它,但是,您可以在没有错误/警告的情况下重新定义$ STDOUT(重新定义STDOUT将引起警告)。例如,你可以这样做:
$stdout = STDERR
Same goes for $stderr
and STDERR
$stderr和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.
因此,要回答问题的另一部分,使用全局变量来重定向输出,而不是常量。只要在代码中进一步修改它,重新定义全局变量就会影响应用程序的其他部分。
#2
4
-
STDOUT
is a global constant, so it should not be changed. - STDOUT是一个全局常量,所以它不应该被改变。
-
$stdout
is a predefined variable, so it can be changed. - $stdout是一个预定义的变量,因此可以更改它。
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
.
但是,如果您试图在您的Ruby脚本中更改操作系统STDOUT的文件描述符,例如,根据当前日期将输出发送到一组旋转的日志文件,那么您将希望确保使用$ STDOUT。
#3
3
Both $stdout
and STDOUT
have different meanings. Ruby's documentation is pretty clear on this topic:
$stdout和stdout都有不同的含义。Ruby在这个主题上的文档非常清晰:
$stdout
– The current standard output.- $stdout -当前标准输出。
STDOUT
– The standard output. The default value for $stdout.- 标准输出。$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。
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
.
STDOUT不是无用的。它存储$stdout的默认值。如果重新分配$stdout,那么可以使用$stdout = stdout将其恢复到以前的值。
Furthermore, there's one more predefined variable:
此外,还有一个预定义变量:
$>
– The default output for print, printf, which is$stdout
by default.- $> -打印的默认输出printf,默认为$stdout。
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.
但是在Ruby 2.3中,它只是作为$stdout的别名。重新分配$stdout会改变$>的值,反之亦然。
#1
93
$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
.
$stdout是表示当前标准输出的全局变量。STDOUT是一个表示标准输出的常量,通常是$ 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是一个常量,您不应该重新定义它,但是,您可以在没有错误/警告的情况下重新定义$ STDOUT(重新定义STDOUT将引起警告)。例如,你可以这样做:
$stdout = STDERR
Same goes for $stderr
and STDERR
$stderr和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.
因此,要回答问题的另一部分,使用全局变量来重定向输出,而不是常量。只要在代码中进一步修改它,重新定义全局变量就会影响应用程序的其他部分。
#2
4
-
STDOUT
is a global constant, so it should not be changed. - STDOUT是一个全局常量,所以它不应该被改变。
-
$stdout
is a predefined variable, so it can be changed. - $stdout是一个预定义的变量,因此可以更改它。
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
.
但是,如果您试图在您的Ruby脚本中更改操作系统STDOUT的文件描述符,例如,根据当前日期将输出发送到一组旋转的日志文件,那么您将希望确保使用$ STDOUT。
#3
3
Both $stdout
and STDOUT
have different meanings. Ruby's documentation is pretty clear on this topic:
$stdout和stdout都有不同的含义。Ruby在这个主题上的文档非常清晰:
$stdout
– The current standard output.- $stdout -当前标准输出。
STDOUT
– The standard output. The default value for $stdout.- 标准输出。$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。
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
.
STDOUT不是无用的。它存储$stdout的默认值。如果重新分配$stdout,那么可以使用$stdout = stdout将其恢复到以前的值。
Furthermore, there's one more predefined variable:
此外,还有一个预定义变量:
$>
– The default output for print, printf, which is$stdout
by default.- $> -打印的默认输出printf,默认为$stdout。
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.
但是在Ruby 2.3中,它只是作为$stdout的别名。重新分配$stdout会改变$>的值,反之亦然。