# Input redirection in ruby

**URL:** <https://rubytalk.org/t/input-redirection-in-ruby/48032>\
**Category:** ruby-talk\
**Created:** [31 July 2008 14:10 UTC](https://rubytalk.org/t/input-redirection-in-ruby/48032 "2008-07-31T14:10:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michal\_hramrach\_Such](https://avatars.discourse-cdn.com/v4/letter/m/3ab097/32.png) [@Michal\_hramrach\_Such](https://rubytalk.org/u/Michal_hramrach_Such)\
**Post date:** [31 July 2008 14:10 UTC](https://rubytalk.org/t/input-redirection-in-ruby/48032/1 "2008-07-31T14:10:10Z")

</div>

Hello

I could not find any way to redirect input and output of programs in ruby.

You can capture the fds but not redirect AFAICT.

So unless there is something I have overlooked I add the the ruby wishlist:

let popen and popen3 take IO arguments for use as stdin, stdout, and  
stderr. Since they can be named in 1.9 (or was that dropped?) you may  
specify anything you want. These should probably work only with IO  
that is backed by a real fd, support for StringIO or similar might be  
problematic.

Thanks

Michal

---

<div class="post-metadata">

**Author:** ![Shadowfirebird](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@Shadowfirebird](https://rubytalk.org/u/Shadowfirebird)\
**Post date:** [31 July 2008 14:27 UTC](https://rubytalk.org/t/input-redirection-in-ruby/48032/2 "2008-07-31T14:27:13Z")

</div>

Well, you can say $stderr.puts "message", for example. But I'm  
guessing that you are talking about something more comprehensive?

> **···**
>
> On Thu, Jul 31, 2008 at 3:10 PM, Michal Suchanek \<hramrach@centrum.cz\> wrote:
> 
> > Hello
> > 
> > I could not find any way to redirect input and output of programs in ruby.
> > 
> > You can capture the fds but not redirect AFAICT.
> > 
> > So unless there is something I have overlooked I add the the ruby wishlist:
> > 
> > let popen and popen3 take IO arguments for use as stdin, stdout, and  
> > stderr. Since they can be named in 1.9 (or was that dropped?) you may  
> > specify anything you want. These should probably work only with IO  
> > that is backed by a real fd, support for StringIO or similar might be  
> > problematic.
> > 
> > Thanks
> > 
> > Michal
> 
> --  
> Me, I imagine places that I have never seen / The colored lights in  
> fountains, blue and green / And I imagine places that I will never go  
> / Behind these clouds that hang here dark and low  
> But it's there when I'm holding you / There when I'm sleeping too /  
> There when there's nothing left of me / Hanging out behind the  
> burned-out factories / Out of reach but leading me / Into the  
> beautiful sea

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [31 July 2008 17:19 UTC](https://rubytalk.org/t/input-redirection-in-ruby/48032/3 "2008-07-31T17:19:22Z")

</div>

You can - and it's pretty easy also. The key feature to know is IO#reopen:

robert@fussel ~  
$ rm x

robert@fussel ~  
$ ./ro.rb  
foo 1  
foo 2

robert@fussel ~  
$ head x  
insgesamt 4319  
drwx------+ 22 robert Kein 0 Feb 17 16:27 Anwendungsdaten  
drwx------+ 2 robert Kein 0 Jul 27 23:24 Cookies  
drwx------+ 12 robert Kein 0 Jul 27 14:18 Desktop  
drwx------+ 2 robert Kein 0 Aug 18 2004 Druckumgebung  
drwx------+ 17 robert Kein 0 Apr 20 12:14 Eigene Dateien  
drwx------+ 5 robert Kein 0 Feb 8 19:58 Favoriten  
drwx------+ 6 robert Kein 0 Sep 17 2006 Lokale Einstellungen  
-rwx------+ 1 robert Kein 4194304 Jul 27 12:07 NTUSER.DAT  
drwx------+ 2 robert Kein 0 Mar 24 18:05 Netzwerkumgebung

robert@fussel ~  
$ cat ro.rb  
#!/bin/env ruby

puts "foo 1"

fork do  
&nbsp;&nbsp;&nbsp;STDOUT.reopen("x", "w")  
&nbsp;&nbsp;&nbsp;exec "ls", "-l"  
end

puts "foo 2"

robert@fussel ~  
$

Alternatively you can use the shell's redirection feature when invoking system with one argument only:

robert@fussel ~  
$ ruby -e 'system "ls -l \> xx"'

robert@fussel ~  
$ head xx  
insgesamt 4323  
drwx------+ 22 robert Kein 0 Feb 17 16:27 Anwendungsdaten  
drwx------+ 2 robert Kein 0 Jul 27 23:24 Cookies  
drwx------+ 12 robert Kein 0 Jul 27 14:18 Desktop  
drwx------+ 2 robert Kein 0 Aug 18 2004 Druckumgebung  
drwx------+ 17 robert Kein 0 Apr 20 12:14 Eigene Dateien  
drwx------+ 5 robert Kein 0 Feb 8 19:58 Favoriten  
drwx------+ 6 robert Kein 0 Sep 17 2006 Lokale Einstellungen  
-rwx------+ 1 robert Kein 4194304 Jul 27 12:07 NTUSER.DAT  
drwx------+ 2 robert Kein 0 Mar 24 18:05 Netzwerkumgebung

robert@fussel ~  
$

Cheers

&nbsp;&nbsp;robert

> **···**
>
> On 31.07.2008 16:10, Michal Suchanek wrote:
> 
> > I could not find any way to redirect input and output of programs in ruby.
> > 
> > You can capture the fds but not redirect AFAICT.

---

<div class="post-metadata">

**Author:** ![Michal\_hramrach\_Such](https://avatars.discourse-cdn.com/v4/letter/m/3ab097/32.png) [@Michal\_hramrach\_Such](https://rubytalk.org/u/Michal_hramrach_Such)\
**Post date:** [31 July 2008 23:10 UTC](https://rubytalk.org/t/input-redirection-in-ruby/48032/4 "2008-07-31T23:10:30Z")

</div>

Yes, this makes stuff much easier. Still to do what shell does when  
you use a shell pipe you need a working fork. I am not sure how fork  
is emulated on platforms that use spawn like Windows.

According to porting tutorials passing the fd's is possible with the  
windows API but doing a full fork() is hard - it normally requires  
copying the whole address space manually because part of the NT core  
functionality is not exposed in the Win32 API.

Thanks

Michal

> **···**
>
> On 31/07/2008, Robert Klemme \<shortcutter@googlemail.com\> wrote:
> 
> > On 31.07.2008 16:10, Michal Suchanek wrote:
> > 
> > \> I could not find any way to redirect input and output of programs in ruby.  
> > \>  
> > \> You can capture the fds but not redirect AFAICT.  
> > \>
> > 
> > You can - and it's pretty easy also. The key feature to know is IO#reopen:

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [1 August 2008 06:47 UTC](https://rubytalk.org/t/input-redirection-in-ruby/48032/5 "2008-08-01T06:47:24Z")

</div>

Well, I was actually on Windows (although I have to admit that I use  
the cygwin version all the time). 🙂

Can't remember: do IO.popen and friends work in the Windows version?  
If it does, then you got your redirection with pipes already.

Kind regards

robert

> **···**
>
> 2008/8/1 Michal Suchanek \<hramrach@centrum.cz\>:
> 
> > On 31/07/2008, Robert Klemme \<shortcutter@googlemail.com\> wrote:
> > 
> > > On 31.07.2008 16:10, Michal Suchanek wrote:
> > > 
> > > \> I could not find any way to redirect input and output of programs in ruby.  
> > > \>  
> > > \> You can capture the fds but not redirect AFAICT.  
> > > \>
> > > 
> > > You can - and it's pretty easy also. The key feature to know is IO#reopen:
> > 
> > Yes, this makes stuff much easier. Still to do what shell does when  
> > you use a shell pipe you need a working fork. I am not sure how fork  
> > is emulated on platforms that use spawn like Windows.
> 
> --  
> use.inject do |as, often| as.you\_can - without end
