On 13 May 2011 15:48, Roger Pack <rogerpack2005 / gmail.com> wrote:
> Roger Pack wrote in post #998434:
>> In the spirit of the recent call for better ruby documentation, I wrote
>> a
>> writeup:
>>
>>
> http://en.wikibooks.org/w/index.php?title=Ruby_Programming/Running_Multiple_Processes&stable=0#Chaining_processes
>
>
> Unfortunately it appears the wiki formatting is mangled in firefox, but
> works well in chrome :)
>
> Here's the complete code snippet:
>
>
> pipe_me_in, pipe_peer_out = IO.pipe
> pipe_peer_in, pipe_me_out = IO.pipe
>
> fork do
> STDIN.reopen(pipe_peer_in)
> STDOUT.reopen(pipe_peer_out)
> Kernel.exec("echo 33")
> # this line is never executed because exec moves the process
> end
> pipe_peer_out.close # file handles have to all be closed in order for
> the "read" method, below, to be able to know
> # that it's done reading data, so it can return. See also
> http://devver.wordpress.com/2009/10/22/beware-of-pipe-duplication-in-subprocesses/
> pipe_me_in.read

Changing the last line to

puts '"' + pipe_me_in.read.chomp + '"'

Really writes 33 quoted so IO.reopen works for setting up the file descriptors.

It is not clear from the docs if it would work or not.

It does work, though:

out = File.open("testfile","w")

STDOUT.reopen(out)

system("echo 33")

STDERR.puts '"' + File.read("testfile").chomp + '"'

=> "33"

I vaguely recall some issues with using STDIN and STDOUT for
redirection earlier but it might be due to using them incorrectly.

>
>
>
> There is a link in there to a blog describing ruby's shell class,
> perhaps you did not see it?

Yes, I missed the link labeled just [1]


Thanks

Michal