tags:

views:

70

answers:

1

For what purpose are there $deferr, $defout and why there is no $defin if there are $stderr, $stdout and $stdin, and also STDIN, STDOUT and STDERR

Changing any of them will not change others

+5  A: 

STDIN, STDOUT and STDERR are global stream constants (i.e. default values).

$stdin, $stdout and $stderr are global variables initialised to the value of the stream constants. This allows you to change their values at runtime (e.g. to change stdout to an alternative output device).

$defout is a way of creating an 'in-place' output stream. There is a brief discussion on the ruby mailing list here:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/67822

Obviously you can't have an in-place input stream, so there is no '$defin'.

However, this is mostly for historical interest; I believe $defout and $deferr were deprecated some time ago with the release of Ruby 1.8.2, and current best practice is to use only $stdin, $stdout and $stderr.

ire_and_curses
If I execute lines from ruby-talk page but add some more ruby code<code>ruby -p -i.bak -e 'STDERR.puts $stdout, $defout, $stderr, $deferr; $_.upcase!' junk</code>,then I see that $stdout is equal to $defout and $stderr is equal to $deferr.Also if I execute <code>ruby -e '$stdout = File.open("junk", "w"); print "output"'</code>,then string "output" will be in file junk.Can I asume that it is better just to forget about $defout and $deferr as they are deprecated?
tig
Yep - that's what I'd do. Feel free to accept (tick) this answer if it is helpful!
ire_and_curses