STDIN, OUT, ERR and $stdin, out, err - Differences?


#1

Can anyone explain what’s happening here?

print $stdin.fileno => 0
print $stdout.fileno => 0
print $sterr.fileno => 0

print STDIN.fileno => 0
print STDOUT.fileno => 1
print STDERR.fileno => 2

print STDOUT.object_id => 21673330
print $stdout.object_id => 21673330

I get the same results in and out of eclipse and with irb

ruby 1.8.6, XP

Many thanks.


#2

Terry C. wrote:

Can anyone explain what’s happening here?

print $stdin.fileno => 0
print $stdout.fileno => 0
print $sterr.fileno => 0

print STDIN.fileno => 0
print STDOUT.fileno => 1
print STDERR.fileno => 2

print STDOUT.object_id => 21673330
print $stdout.object_id => 21673330

I get the same results in and out of eclipse and with irb

ruby 1.8.6, XP

Many thanks.

Simply, STDIN, STDOUT, STDERR are CONSTANTS and
$stdin, $stdout, and $stderr are global variables.

They are the same initially but the variables can
be re-assigned.


#3

On Jun 5, 10:14 pm, Terry C. removed_email_address@domain.invalid wrote:

print STDOUT.object_id => 21673330
print $stdout.object_id => 21673330

I get the same results in and out of eclipse and with irb

ruby 1.8.6, XP

Many thanks.

Differences between $stdout and STDOUT:

http://blog.segment7.net/articles/2006/08/17/stdout-vs-stdout

The bottom line is don’t mess with STDOUT and use $stdout instead.

Regards,


#4

Terry C. wrote:

Can anyone explain what’s happening here?

print $stdin.fileno => 0
print $stdout.fileno => 0
print $sterr.fileno => 0

Copy-paste error? The third line is clearly a typo for $stderr, and
under Linux at least these give 0, 1, 2 respectively.


#5

Janos S. wrote:

On Sun, Jun 7, 2009 at 7:34 PM, Brian C. removed_email_address@domain.invalid
wrote:

Copy-paste error? The third line is clearly a typo for $stderr, and
under Linux at least these give 0, 1, 2 respectively.

I can confirm this is true on WinXP, Ruby 1.8.7 and 1.9.1

That makes no sense. The OP said that $stdout.fileno != STDOUT.fileno,
and yet $stdout.object_id == STDOUT.object_id

It can’t be the same object, but respond to the same method differently.

What do you get from this? Run it as a standalone ruby script, not
within irb, to eliminate irb as the source of the problem.

puts STDOUT.fileno
puts $stdout.fileno
puts STDOUT.inspect
puts $stdout.inspect

and repeat for STDERR/$stderr


#6

On Sun, Jun 7, 2009 at 7:34 PM, Brian C. removed_email_address@domain.invalid
wrote:

Copy-paste error? The third line is clearly a typo for $stderr, and
under Linux at least these give 0, 1, 2 respectively.

Posted via http://www.ruby-forum.com/.

I can confirm this is true on WinXP, Ruby 1.8.7 and 1.9.1


#7

Maybe I misunderstood, and you were confirming my interpretation to be
true, not the OP’s :slight_smile:

:wink: Yes, I meant you were right; it’s working as intended (0, 1, 2 ) on
windows.


#8

Brian C. wrote:

Janos S. wrote:

On Sun, Jun 7, 2009 at 7:34 PM, Brian C. removed_email_address@domain.invalid
wrote:

Copy-paste error? The third line is clearly a typo for $stderr, and
under Linux at least these give 0, 1, 2 respectively.

I can confirm this is true on WinXP, Ruby 1.8.7 and 1.9.1

Maybe I misunderstood, and you were confirming my interpretation to be
true, not the OP’s :slight_smile:

Anyway I’ve just booted a laptop into XP, and done this:

C:>ruby
puts STDOUT.fileno
puts $stdout.fileno
puts STDOUT.inspect
puts $stdout.inspect
puts STDERR.fileno
puts $stderr.fileno
puts STDERR.inspect
puts $stderr.inspect
^Z
1
1
#IO:0x2846af0
#IO:0x2846af0
2
2
#IO:0x2846adc
#IO:0x2846adc

C:>ruby -v
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]

This version of Ruby came from the ‘one-click installer’ Ruby-186-26

And FWIW, I get the same in irb too.

HTH,

Brian.