STDERR.puts 与 Ruby 中的 puts 有何不同?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22310012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How is STDERR.puts different from puts in Ruby?
提问by rubynewbie
I'm learning the language from Programming Ruby 1.9, and they toss STDERR.puts into a block of code early in the book without explaining why they're using it or how it's different from puts.
我正在从 Ruby 编程 1.9 中学习这门语言,他们在本书的早期将 STDERR.puts 放入代码块中,而没有解释为什么使用它或它与 puts 有何不同。
I've googled and wikied the term, but all I can gather from my research is that it's involved in diagnostics. Nowhere in the code provided by Programming Ruby does there seem to be a link to error exception handling.
我已经用谷歌搜索并维基了这个术语,但我从我的研究中可以收集到的只是它涉及诊断。Programming Ruby 提供的代码中似乎没有任何地方指向错误异常处理的链接。
Here's the code.
这是代码。
require_relative 'csv_reader'
reader = CsvReader.new
ARGV.each do |csv_file_name|
STDERR.puts "Processing #{csv_file_name}"
reader.read_in_csv_data(csv_file_name)
end
I did manage to read somewhere that STDERR.puts is used for error handling out of convention, but I guess I'm asking if it acts any differently than puts.
我确实设法在某处读到 STDERR.puts 用于超出约定的错误处理,但我想我在问它的行为是否与 puts 有任何不同。
回答by Palpatim
By default, putswrites to STDOUT. By specifying STDERR.puts, you're sending your output to the STDERR handle. Although the implementation behavior is the same, using STDERR instead of STDOUT will definitely impact consumers of your program, since they will be attempting to capture output from your program from STDOUT, by convention. Best practice is to log debugging information, errors, warnings, status, etc to STDERR and actual program output to STDOUT.
默认情况下,puts写入 STDOUT。通过指定STDERR.puts,您将输出发送到 STDERR 句柄。尽管实现行为是相同的,但使用 STDERR 而不是 STDOUT 肯定会影响程序的使用者,因为按照惯例,他们将尝试从 STDOUT 捕获程序的输出。最佳实践是将调试信息、错误、警告、状态等记录到 STDERR,并将实际程序输出记录到 STDOUT。
回答by Steve Weet
Within a *nix based system when a new process is started it will by default have three open filehandles these are as follows
在基于 *nix 的系统中,当一个新进程启动时,默认情况下它会有三个打开的文件句柄,如下所示
0 - STDIN 1 - STDOUT 2 - STDERR
These numbers, lets call them file descriptors are important to the unix shell that you are using to start your program. STDIN is where your program expects to get its input from (Usually the keyboard unless you've changed that). STDOUT is where your program will write its output (Usually the screen unless you've changed it) and STDERR is where it will write its errors (Again usually the screen unless you've changed it).
这些数字,让我们称它们为文件描述符对于您用来启动程序的 unix shell 很重要。STDIN 是您的程序希望从中获取输入的地方(通常是键盘,除非您已更改)。STDOUT 是您的程序将写入其输出的地方(通常是屏幕,除非您已更改它),而 STDERR 是它将写入其错误的地方(同样通常是屏幕,除非您已更改它)。
The common statement from above is "Unless you've changed it". If you run a command like this
上面的共同陈述是“除非你改变了它”。如果你运行这样的命令
command > file.out
Then the output (everything written to STDOUT) will not show up on screen it will appear in file.out. If you run your command like this
然后输出(写入 STDOUT 的所有内容)将不会显示在屏幕上,而是会出现在 file.out 中。如果你像这样运行你的命令
command 2> file.err
Then your output will appear on screen again but any errors written to STDERR will appear in file.err. In fact command > file.outis really shorthand for command 1>file.out. Whatever applies to the > symbol (Redirection) also applies to the | symbol for piping. This means that if you run your program as a filter, receiving data on STDIN and writing to STDOUT then other programs can receive their data from your program but they won't receive the data written to STDERR. (Unless you've asked it to)
然后您的输出将再次出现在屏幕上,但写入 STDERR 的任何错误都将出现在 file.err 中。实际上command > file.out真的是 的简写command 1>file.out。适用于 > 符号(重定向)的任何内容也适用于 | 管道的符号。这意味着如果您将程序作为过滤器运行,在 STDIN 上接收数据并写入 STDOUT,那么其他程序可以从您的程序接收数据,但不会接收写入 STDERR 的数据。(除非你要求它)
You can use both of these commands together like this.
您可以像这样一起使用这两个命令。
command 1> file.out 2> file.err # Generally you would leave out the 1
In this instance, not surprisingly the output and error will be in 2 separate files. Bash also has the notion of duplicating file descriptors. This is done with >& operator. The following command will put both STDOUT and STDERR in the same file.
在这种情况下,毫不奇怪,输出和错误将在 2 个单独的文件中。Bash 也有复制文件描述符的概念。这是通过 >& 运算符完成的。以下命令会将 STDOUT 和 STDERR 放在同一个文件中。
command > file.out 2>&1
What this command is saying is run the command setting up STDOUT to be redirected to file.out then duplicate ( >& ) file descriptor 2 (STDERR) to go to wherever file descriptor 1 is going to (file.out)
该命令的意思是运行命令设置 STDOUT 以重定向到 file.out 然后复制 ( >& ) 文件描述符 2 (STDERR) 以转到文件描述符 1 要去的任何地方 (file.out)
You need to be a little careful here with the order that you use these arguments in. Compare the above with this command.
在此您需要注意使用这些参数的顺序。将上面的命令与此命令进行比较。
command 2>&1 > file.out
This has an entirely different result. This command says run the command and duplicate STDERR on to STDOUT (At this point in time the terminal) then redirect STDOUT to file.out. Your error text will remain on the screen.
这有一个完全不同的结果。此命令表示运行该命令并将 STDERR 复制到 STDOUT(此时是终端),然后将 STDOUT 重定向到 file.out。您的错误文本将保留在屏幕上。
回答by Matheus Moreira
Each program starts with three standard file descriptors when they're spawned: standard input, output and error streams.
每个程序在生成时都以三个标准文件描述符开始:标准输入、输出和错误流。
The standard input stream is a generic stream from which program input should be read. Likewise, the standard output stream is a generic stream to which program output may be written. So, what is standard error?
标准输入流是应该从中读取程序输入的通用流。同样,标准输出流是可以写入程序输出的通用流。那么,什么是标准误呢?
The difference between standard output and standard error is subtle but extremely important in UNIX systems due to how programs are used together via pipes. Programs are often designed to consume the output of other programs as their input; this is done by redirecting the standard output of one program to the standard input of another, often via the pipe operator (|). This leaves standard error still connected to the terminal. The user can choose to view the data sent to that stream in the terminal itself, or redirect it to a log file, or to /dev/null, anything the user desires. Note that the data sent to stderrneeds not necessarily be error messages; it's just data that is separate from the actual output of the program.
由于程序如何通过管道一起使用,标准输出和标准错误之间的区别很微妙,但在 UNIX 系统中非常重要。程序通常被设计为使用其他程序的输出作为它们的输入;这是通过将一个程序的标准输出重定向到另一个程序的标准输入来完成的,通常是通过管道运算符 ( |)。这使得标准错误仍然连接到终端。用户可以选择在终端本身中查看发送到该流的数据,或将其重定向到日志文件,或重定向到/dev/null用户想要的任何内容。请注意,发送到的数据stderr不一定是错误消息;它只是与程序的实际输出分开的数据。
Supporting this paradigm is extremely important for the usability of programs, as it provides a predictable user interface towards program input, output and messages. The user can manipulate these as they see fit, and often interesting applications arise in an unforeseen manner.
支持这种范式对于程序的可用性非常重要,因为它为程序输入、输出和消息提供了可预测的用户界面。用户可以按照他们认为合适的方式操作这些,并且经常以不可预见的方式出现有趣的应用程序。
So, generally speaking, standard output is for the actual output and standard error is for communicating with the user.
所以,一般来说,标准输出用于实际输出,标准错误用于与用户通信。

