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.
Terry C. (Guest)
on 2009-06-06 05: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.
Jeff M. (Guest)
on 2009-06-07 02:43
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.
Luis L. (Guest)
on 2009-06-07 04:16
(Received via mailing list)
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/stdou...

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

Regards,
Brian C. (Guest)
on 2009-06-07 21:34
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.
Janos S. (Guest)
on 2009-06-08 12:00
(Received via mailing list)
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
Brian C. (Guest)
on 2009-06-08 16:39
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
Brian C. (Guest)
on 2009-06-08 16:50
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 :-)

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.
Janos S. (Guest)
on 2009-06-09 09: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.