Input redirection in ruby

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

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 S. [email protected]
wrote:

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

On 31.07.2008 16:10, Michal S. 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:

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
STDOUT.reopen(“x”, “w”)
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

robert

2008/8/1 Michal S. [email protected]:

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.

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

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

On 31/07/2008, Robert K. [email protected] wrote:

On 31.07.2008 16:10, Michal S. 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.

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