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

T

Terry Cooper

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.
 
J

Jeff Moore

Terry said:
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.
 
L

Luis Lavena

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.

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,
 
B

Brian Candler

Terry said:
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.
 
J

Janos Sebok

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

--=20
=DCdv,
Sebi
 
B

Brian Candler

Janos said:
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
 
B

Brian Candler

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

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.
 
J

Janos Sebok

Maybe I misunderstood, and you were confirming my interpretation to be
;) Yes, I meant you were right; it's working as intended (0, 1, 2 ) on
windows.


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.


--=20
=DCdv,
Sebi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top