# Stdout as IO object

**URL:** <https://www.ruby-forum.com/t/stdout-as-io-object/191539>\
**Category:** Ruby\
**Created:** [July 14, 2010, 3:41am UTC](https://www.ruby-forum.com/t/stdout-as-io-object/191539 "2010-07-14T03:41:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jnb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df705f/32.png) [@jnb](https://www.ruby-forum.com/u/jnb)\
**Post date:** [July 14, 2010, 3:41am UTC](https://www.ruby-forum.com/t/stdout-as-io-object/191539/1 "2010-07-14T03:41:21Z")

</div>

Okay, real simple question, but somehow I am missing it in the API: how  
do create an IO object that represents stdout? I’m using a certain  
function that receives writable IO objects and I wanted to pass in the  
standard output as a stream.

---

<div class="post-metadata">

**Author:** ![jnb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df705f/32.png) [@jnb](https://www.ruby-forum.com/u/jnb)\
**Post date:** [July 14, 2010, 3:47am UTC](https://www.ruby-forum.com/t/stdout-as-io-object/191539/2 "2010-07-14T03:47:04Z")

</div>

On Wed, Jul 14, 2010 at 4:41 AM, Jonathan B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> ## Okay, real simple question, but somehow I am missing it in the API: how do create an IO object that represents stdout? I’m using a certain function that receives writable IO objects and I wanted to pass in the standard output as a stream.
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Why not just pass $stdout itself?

Ammar

---

<div class="post-metadata">

**Author:** ![jnb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df705f/32.png) [@jnb](https://www.ruby-forum.com/u/jnb)\
**Post date:** [July 14, 2010, 10:10am UTC](https://www.ruby-forum.com/t/stdout-as-io-object/191539/3 "2010-07-14T10:10:03Z")

</div>

Jonathan B. wrote:

> Okay, real simple question, but somehow I am missing it in the API: how  
> do create an IO object that represents stdout? I’m using a certain  
> function that receives writable IO objects and I wanted to pass in the  
> standard output as a stream.

When your program starts, the constant STDOUT is an open file descriptor  
for STDOUT, and the global variable $stdout also points to it.

Using the global variable is the recommended approach, as it adds more  
flexibility for pointing it to a different object like a StringIO  
without having warnings about constants being redefined.

---

<div class="post-metadata">

**Author:** ![jnb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df705f/32.png) [@jnb](https://www.ruby-forum.com/u/jnb)\
**Post date:** [July 14, 2010, 5:57pm UTC](https://www.ruby-forum.com/t/stdout-as-io-object/191539/4 "2010-07-14T17:57:36Z")

</div>

Instead of modifying the global variable $stdout, you could just pass in  
an  
instance of StringIO to your method/class. I passed in two instances of  
StringIO to one of my classes for mocking STDIN and STDOUT.

---

<div class="post-metadata">

**Author:** ![jnb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df705f/32.png) [@jnb](https://www.ruby-forum.com/u/jnb)\
**Post date:** [July 14, 2010, 8:30pm UTC](https://www.ruby-forum.com/t/stdout-as-io-object/191539/5 "2010-07-14T20:30:49Z")

</div>

Brian C. wrote:

> Jonathan B. wrote:
> 
> > Okay, real simple question, but somehow I am missing it in the API: how  
> > do create an IO object that represents stdout? I’m using a certain  
> > function that receives writable IO objects and I wanted to pass in the  
> > standard output as a stream.
> 
> When your program starts, the constant STDOUT is an open file descriptor  
> for STDOUT, and the global variable $stdout also points to it.
> 
> Using the global variable is the recommended approach, as it adds more  
> flexibility for pointing it to a different object like a StringIO  
> without having warnings about constants being redefined.

The $stdout variable works fine. I was a little confused with $stdout at  
first, because the documentation I found either does not describe what  
it is, or talks about it as a “file descriptor”. But it is actually an  
IO object:

$ irb  
irb(main):001:0\> $stdout.class  
=\> IO
