Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use $stderr vs STDERR in Ruby? [duplicate]

Tags:

ruby

Possible Duplicate:
Ruby $stdout vs. STDOUT

Is STDERR generally preferred over using $stderr, or vice versa?

And what about STDOUT vs $stdout?

like image 944
dan Avatar asked Mar 16 '12 23:03

dan


1 Answers

To the contrary, using $stderr is preferrable.

The reason for that is that $stderr as a global variable can be reassigned, while STDERR as a constant shouldn't be reassigned (it raises a warning).

Usually they both point to the same standard error file, but in some cases you might want to temporarily redirect all your output somewhere else (for example, to a log file or into a string buffer), in which case you can just reassign $stderr and all your code will respect that (if you were smart enough to use $stderr instead of STDERR in the first place).

like image 89
Niklas B. Avatar answered Oct 24 '22 17:10

Niklas B.