From: Jonathan Bale on 13 Jul 2010 21:41 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/.
From: Ammar Ali on 13 Jul 2010 21:46 [Note: parts of this message were removed to make it a legal post.] On Wed, Jul 14, 2010 at 4:41 AM, Jonathan Bale <webmaster(a)indicium.us>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/. > > Why not just pass $stdout itself? Ammar
From: Brian Candler on 14 Jul 2010 04:10 Jonathan Bale 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. -- Posted via http://www.ruby-forum.com/.
From: skim on 14 Jul 2010 11:56 [Note: parts of this message were removed to make it a legal post.] 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. On Wed, Jul 14, 2010 at 03:10, Brian Candler <b.candler(a)pobox.com> wrote: > Jonathan Bale 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. > -- > Posted via http://www.ruby-forum.com/. > >
From: Jonathan Bale on 14 Jul 2010 14:30 Brian Candler wrote: > Jonathan Bale 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 -- Posted via http://www.ruby-forum.com/.
|
Pages: 1 Prev: How to update Ruby >>> CentOS Next: configurability 1.0.0 |