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

**URL:** <https://www.ruby-forum.com/t/stdin-out-err-and-stdin-out-err-differences/169639>\
**Category:** Ruby\
**Created:** [June 6, 2009, 3:14am UTC](https://www.ruby-forum.com/t/stdin-out-err-and-stdin-out-err-differences/169639 "2009-06-06T03:14:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![terrychou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecc23a/32.png) [@terrychou](https://www.ruby-forum.com/u/terrychou)\
**Post date:** [June 6, 2009, 3:14am UTC](https://www.ruby-forum.com/t/stdin-out-err-and-stdin-out-err-differences/169639/1 "2009-06-06T03:14:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![terrychou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecc23a/32.png) [@terrychou](https://www.ruby-forum.com/u/terrychou)\
**Post date:** [June 7, 2009, 12:43am UTC](https://www.ruby-forum.com/t/stdin-out-err-and-stdin-out-err-differences/169639/2 "2009-06-07T00:43:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![terrychou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecc23a/32.png) [@terrychou](https://www.ruby-forum.com/u/terrychou)\
**Post date:** [June 7, 2009, 2:16am UTC](https://www.ruby-forum.com/t/stdin-out-err-and-stdin-out-err-differences/169639/3 "2009-06-07T02:16:45Z")

</div>

On Jun 5, 10:14 pm, Terry C. [removed\_email\_address@domain.invalid](mailto: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](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,

---

<div class="post-metadata">

**Author:** ![terrychou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecc23a/32.png) [@terrychou](https://www.ruby-forum.com/u/terrychou)\
**Post date:** [June 7, 2009, 7:34pm UTC](https://www.ruby-forum.com/t/stdin-out-err-and-stdin-out-err-differences/169639/4 "2009-06-07T19:34:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![terrychou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecc23a/32.png) [@terrychou](https://www.ruby-forum.com/u/terrychou)\
**Post date:** [June 8, 2009, 2:39pm UTC](https://www.ruby-forum.com/t/stdin-out-err-and-stdin-out-err-differences/169639/5 "2009-06-08T14:39:48Z")

</div>

Janos S. wrote:

> On Sun, Jun 7, 2009 at 7:34 PM, Brian C. [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![terrychou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecc23a/32.png) [@terrychou](https://www.ruby-forum.com/u/terrychou)\
**Post date:** [June 8, 2009, 10:00am UTC](https://www.ruby-forum.com/t/stdin-out-err-and-stdin-out-err-differences/169639/6 "2009-06-08T10:00:59Z")

</div>

On Sun, Jun 7, 2009 at 7:34 PM, Brian C. [removed\_email\_address@domain.invalid](mailto: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/](http://www.ruby-forum.com/).

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

---

<div class="post-metadata">

**Author:** ![terrychou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecc23a/32.png) [@terrychou](https://www.ruby-forum.com/u/terrychou)\
**Post date:** [June 9, 2009, 7:48am UTC](https://www.ruby-forum.com/t/stdin-out-err-and-stdin-out-err-differences/169639/7 "2009-06-09T07:48:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![terrychou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecc23a/32.png) [@terrychou](https://www.ruby-forum.com/u/terrychou)\
**Post date:** [June 8, 2009, 2:50pm UTC](https://www.ruby-forum.com/t/stdin-out-err-and-stdin-out-err-differences/169639/8 "2009-06-08T14:50:18Z")

</div>

Brian C. wrote:

> Janos S. wrote:
> 
> > On Sun, Jun 7, 2009 at 7:34 PM, Brian C. [removed\_email\_address@domain.invalid](mailto: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.
