End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
class method
Open3.pipeline_rw
pipeline_rw(*cmds, &block)
lib/open3.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.1.3/lib/open3.rb, line 463
def pipeline_rw(*cmds, &block)
if Hash === cmds.last
opts = cmds.pop.dup
else
opts = {}
end
in_r, in_w = IO.pipe
opts[:in] = in_r
in_w.sync = true
out_r, out_w = IO.pipe
opts[:out] = out_w
pipeline_run(cmds, opts, [in_r, out_w], [in_w, out_r], &block)
end
Open3.pipeline_rw starts a list of commands as a pipeline with pipes which connect to stdin of the first command and stdout of the last command.
Open3.pipeline_rw(cmd1, cmd2, ... [, opts]) {|first_stdin, last_stdout, wait_threads|
...
}
first_stdin, last_stdout, wait_threads = Open3.pipeline_rw(cmd1, cmd2, ... [, opts])
...
first_stdin.close
last_stdout.close
Each cmd is a string or an array. If it is an array, the elements are passed to Process.spawn.
cmd: commandline command line string which is passed to a shell [env, commandline, opts] command line string which is passed to a shell [env, cmdname, arg1, ..., opts] command name and one or more arguments (no shell) [env, [cmdname, argv0], arg1, ..., opts] command name and arguments including argv[0] (no shell) Note that env and opts are optional, as for Process.spawn.
The options to pass to Process.spawn are constructed by merging opts, the last hash element of the array, and specifications for the pipes between each of the commands.
Example:
Example
Open3.pipeline_rw("tr -dc A-Za-z", "wc -c") {|i, o, ts| i.puts "All persons more than a mile high to leave the court." i.close p o.gets #=> "42\n" } Open3.pipeline_rw("sort", "cat -n") {|stdin, stdout, wait_thrs| stdin.puts "foo" stdin.puts "bar" stdin.puts "baz" stdin.close # send EOF to sort. p stdout.read #=> " 1\tbar\n 2\tbaz\n 3\tfoo\n" }
See also
#pipeline_rw
.pipeline_r
.pipeline_start
.pipeline
also in
4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0