Writing to a process pipe

L

Luke A. Kanies

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
 
A

Ara.T.Howard

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

--
===============================================================================
| 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
===============================================================================
 
T

ts

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
 
L

Luke A. Kanies

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top