# Redirect STDOUT to function?

**URL:** <https://www.ruby-forum.com/t/redirect-stdout-to-function/175877>\
**Category:** Ruby\
**Created:** [September 22, 2009, 3:07am UTC](https://www.ruby-forum.com/t/redirect-stdout-to-function/175877 "2009-09-22T03:07:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![makki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b4bc9f/32.png) [@makki](https://www.ruby-forum.com/u/makki)\
**Post date:** [September 22, 2009, 3:07am UTC](https://www.ruby-forum.com/t/redirect-stdout-to-function/175877/1 "2009-09-22T03:07:53Z")

</div>

Hello,  
I’m using Ruby as a scripting language for a program that supports the  
WSH scripting interface, and I have a small problem: ‘puts’ and other  
output functions don’t work (because STDOUT is invalid), as with ‘gets’  
(but with STDIN). I’d like to redirect all accesses to STDOUT and STDIN  
to my own functions, which would handle the IO through the main  
application’s scripting interface.  
I know that $stdout and others can be reassigned to other IO objects,  
but I haven’t yet found a way to reliably redirect through a function  
instead. Of course, this is probably because I’m relatively new to Ruby.  
Does anyone know how I could do something like this?~Jonathan C.

---

<div class="post-metadata">

**Author:** ![makki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b4bc9f/32.png) [@makki](https://www.ruby-forum.com/u/makki)\
**Post date:** [September 22, 2009, 3:45am UTC](https://www.ruby-forum.com/t/redirect-stdout-to-function/175877/2 "2009-09-22T03:45:20Z")

</div>

On Mon, Sep 21, 2009 at 6:07 PM, Jonathan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I know that $stdout and others can be reassigned to other IO objects, but I haven’t yet found a way to reliably  
> redirect through a function instead. Of course, this is probably because I’m relatively new to Ruby.  
> Does anyone know how I could do something like this?~Jonathan C.

I haven’t tried this, but you should be able to create an IO-like  
class/object where the methods you care about are (or call) the custom  
functions you want to use. It would be nice if there was an module you  
could mix into an object that implemented a few of the more basic  
function which would build on those to provide the rest of the IO  
interface, the way Enumerable builds on an “each” method in the class  
its mixed into.

---

<div class="post-metadata">

**Author:** ![makki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b4bc9f/32.png) [@makki](https://www.ruby-forum.com/u/makki)\
**Post date:** [September 22, 2009, 4:01am UTC](https://www.ruby-forum.com/t/redirect-stdout-to-function/175877/3 "2009-09-22T04:01:43Z")

</div>

On the module suggestion, that would definitely be pretty great! Also,  
I’ve already considered the IO-like class, and I’m just not sure where  
to start. I don’t know what method(s) puts/p/gets/et cetera depend on,  
or what the dependencies depend on; it’s a fairly difficult problem for  
a newcomer to Ruby like me.

---

<div class="post-metadata">

**Author:** ![makki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b4bc9f/32.png) [@makki](https://www.ruby-forum.com/u/makki)\
**Post date:** [September 22, 2009, 4:12am UTC](https://www.ruby-forum.com/t/redirect-stdout-to-function/175877/4 "2009-09-22T04:12:51Z")

</div>

On Tue, 22 Sep 2009 10:07:16 +0900, Jonathan C. wrote:

> could do something like this?~Jonathan C.  
> Create a brand new IO object that’s backed by the function you want.  
> (Maybe a thread and a StringIO would do the trick.)

–Ken

---

<div class="post-metadata">

**Author:** ![makki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b4bc9f/32.png) [@makki](https://www.ruby-forum.com/u/makki)\
**Post date:** [September 22, 2009, 4:22am UTC](https://www.ruby-forum.com/t/redirect-stdout-to-function/175877/5 "2009-09-22T04:22:12Z")

</div>

Hmm, what do you mean by “backed by the function you want”? My whole  
problem is that I don’t know what function(s) to redefine in order for  
the object to work reliably. For example, do all of the methods rely on  
just one to access the data directly, or are there multiple such methods  
I’d need to account for?

---

<div class="post-metadata">

**Author:** ![makki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b4bc9f/32.png) [@makki](https://www.ruby-forum.com/u/makki)\
**Post date:** [September 22, 2009, 11:51pm UTC](https://www.ruby-forum.com/t/redirect-stdout-to-function/175877/6 "2009-09-22T23:51:11Z")

</div>

Why not just override the puts/gets function to your liking?

irb(main):001:0\> def puts(str)  
irb(main):002:1\> :foo  
irb(main):003:1\> end  
=\> nil  
irb(main):004:0\> puts “Hello World”  
=\> :foo
