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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A40b277666e008a40ee133256e32d28f?d=identicon&s=25 Terry Cooper (redplastic)
on 2009-06-06 03:14
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.
C1b6b5557723c9db912b075e954166d3?d=identicon&s=25 Jeff Moore (djief)
on 2009-06-07 00:43
Terry Cooper 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.
E7cff3cfd41c495e1012227d7dc24202?d=identicon&s=25 Luis Lavena (luislavena)
on 2009-06-07 02:16
(Received via mailing list)
On Jun 5, 10:14 pm, Terry Cooper <terence_coo...@hotmail.com> 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/stdou...

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

Regards,
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-06-07 19:34
Terry Cooper 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.
03346f950a31a8894c855a707b993379?d=identicon&s=25 Janos Sebok (Guest)
on 2009-06-08 10:00
(Received via mailing list)
On Sun, Jun 7, 2009 at 7:34 PM, Brian Candler <b.candler@pobox.com>
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
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-06-08 14:39
Janos Sebok wrote:
> On Sun, Jun 7, 2009 at 7:34 PM, Brian Candler <b.candler@pobox.com>
> 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
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-06-08 14:50
Brian Candler wrote:
> Janos Sebok wrote:
>> On Sun, Jun 7, 2009 at 7:34 PM, Brian Candler <b.candler@pobox.com>
>> 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 :-)

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.
03346f950a31a8894c855a707b993379?d=identicon&s=25 Janos Sebok (Guest)
on 2009-06-09 07:48
(Received via mailing list)
>>Maybe I misunderstood, and you were confirming my interpretation to be
>>true, not the OP's :-)

;) Yes, I meant you were right; it's working as intended (0, 1, 2  ) on
windows.
This topic is locked and can not be replied to.