Writing to a process pipe

Discussion in 'Ruby' started by Luke A. Kanies, Jun 8, 2004.

  1. Okay, I know this is an easy one, but I can't find it documented and I
    can't find an example in any libraries. Yes, I'm sure it's in the
    documentation but I can't find it, and yes I'm sure there are examples,
    but I can't find them.

    I need to write to stdin on another process. In perl, this would look
    like:

    open PIPE, "| process";
    print PIPE "stuff\n";
    close PIPE;

    Of course, I tried a couple different iterations of that:

    File.open("| process", "w") { |file| ... }
    Io_Open("| process", "w") { |file| ... }
    IO.popen("| process", "w") { |file| ... }

    All of them fail, or I would not be sending this. So, what's the secret?
    Like I said, I'm sure it's easy, but I can't find it. Apparently my
    Google-fu is not up to snuff today...

    Thanks,
    Luke

    --
    The easiest way to figure the cost of living is to take your income and
    add ten percent.
    ---------------------------------------------------------------------
    Luke Kanies | http://abstractive.org | http://reductiveconsulting.com
     
    Luke A. Kanies, Jun 8, 2004
    #1
    1. Advertising

  2. Luke A. Kanies

    Ara.T.Howard Guest

    On Tue, 8 Jun 2004, Luke A. Kanies wrote:

    > Okay, I know this is an easy one, but I can't find it documented and I
    > can't find an example in any libraries. Yes, I'm sure it's in the
    > documentation but I can't find it, and yes I'm sure there are examples,
    > but I can't find them.
    >
    > I need to write to stdin on another process. In perl, this would look
    > like:
    >
    > open PIPE, "| process";
    > print PIPE "stuff\n";
    > close PIPE;
    >
    > Of course, I tried a couple different iterations of that:
    >
    > File.open("| process", "w") { |file| ... }
    > Io_Open("| process", "w") { |file| ... }
    > IO.popen("| process", "w") { |file| ... }
    >
    > All of them fail, or I would not be sending this. So, what's the secret?
    > Like I said, I'm sure it's easy, but I can't find it. Apparently my
    > Google-fu is not up to snuff today...
    >
    > Thanks,
    > Luke



    irb(main):001:0> pipe = IO.popen 'cat', 'r+'
    => #<IO:0xb740a4d8>

    irb(main):002:0> pipe.puts 'foobar'
    => nil

    irb(main):003:0> pipe.gets
    => "foobar\n"


    check this out if you need more control:

    http://www.codeforpeople.com/lib/ruby/session/


    -a

    >
    > --
    > The easiest way to figure the cost of living is to take your income and
    > add ten percent.
    > ---------------------------------------------------------------------
    > Luke Kanies | http://abstractive.org | http://reductiveconsulting.com
    >
    >
    >


    --
    ===============================================================================
    | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
    | PHONE :: 303.497.6469
    | A flower falls, even though we love it; and a weed grows, even though we do
    | not love it. --Dogen
    ===============================================================================
     
    Ara.T.Howard, Jun 8, 2004
    #2
    1. Advertising

  3. Luke A. Kanies

    ts Guest

    >>>>> "L" == Luke A Kanies <> writes:

    L> File.open("| process", "w") { |file| ... }
    L> Io_Open("| process", "w") { |file| ... }
    L> IO.popen("| process", "w") { |file| ... }

    svg% ruby -e 'open("| cat", "w") {|f| f.puts "it s just Kernel#open"}'
    it s just Kernel#open
    svg%


    Guy Decoux
     
    ts, Jun 8, 2004
    #3
  4. On Wed, 9 Jun 2004, Ara.T.Howard wrote:

    > irb(main):001:0> pipe = IO.popen 'cat', 'r+'
    > => #<IO:0xb740a4d8>
    >
    > irb(main):002:0> pipe.puts 'foobar'
    > => nil
    >
    > irb(main):003:0> pipe.gets
    > => "foobar\n"


    This is what I was looking for. I don't know why I was so hung up on
    needing the '|' character; opening a pipe with 'w' as an argument worked
    fine. Duh.

    I knew it was easy. Thanks.

    --
    If smiling uses so fewer muscles than frowning, how come it
    hurts my face so much?
    ---------------------------------------------------------------------
    Luke Kanies | http://abstractive.org | http://reductiveconsulting.com
     
    Luke A. Kanies, Jun 8, 2004
    #4
    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. momena

    writing to a pipe on win32

    momena, Jul 23, 2004, in forum: Perl
    Replies:
    1
    Views:
    504
    momena
    Jul 28, 2004
  2. lee, wonsun
    Replies:
    1
    Views:
    504
    Jack Klein
    Nov 2, 2004
  3. Carson
    Replies:
    6
    Views:
    322
    Keith Thompson
    Aug 29, 2004
  4. Pito Salas
    Replies:
    11
    Views:
    255
    Robert Klemme
    Aug 26, 2009
  5. Replies:
    1
    Views:
    246
    Ben Morrow
    Jun 2, 2004
Loading...

Share This Page