Input redirection in ruby

Discussion in 'Ruby' started by Michal Suchanek, Jul 31, 2008.

  1. 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
     
    Michal Suchanek, Jul 31, 2008
    #1
    1. Advertising

  2. 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 <> 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
     
    Shadowfirebird, Jul 31, 2008
    #2
    1. Advertising

  3. 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:

    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
     
    Robert Klemme, Jul 31, 2008
    #3
  4. On 31/07/2008, Robert Klemme <> 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.

    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
     
    Michal Suchanek, Aug 1, 2008
    #4
  5. 2008/8/1 Michal Suchanek <>:
    > On 31/07/2008, Robert Klemme <> 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.


    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

    --
    use.inject do |as, often| as.you_can - without end
     
    Robert Klemme, Aug 1, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MadHatter

    input redirection

    MadHatter, Oct 14, 2003, in forum: Perl
    Replies:
    6
    Views:
    715
    MadHatter
    Oct 16, 2003
  2. Dan Upton

    fork/exec with input redirection

    Dan Upton, Nov 26, 2007, in forum: Python
    Replies:
    3
    Views:
    428
    Dan Upton
    Nov 27, 2007
  3. candide

    Input redirection with IDLE

    candide, Oct 13, 2009, in forum: Python
    Replies:
    0
    Views:
    300
    candide
    Oct 13, 2009
  4. Steven Kah Hien Wong

    Ruby redirection woes

    Steven Kah Hien Wong, Oct 26, 2004, in forum: Ruby
    Replies:
    10
    Views:
    216
    Steven Kah Hien Wong
    Nov 4, 2004
  5. Jan 'jast' Krueger
    Replies:
    0
    Views:
    93
    Jan 'jast' Krueger
    Aug 30, 2005
Loading...

Share This Page