Skip to content

Commit 577bee9

Browse files
[DOC] RDoc for Open3 (#19)
1 parent 9f3f5d0 commit 577bee9

File tree

1 file changed

+57
-32
lines changed

1 file changed

+57
-32
lines changed

lib/open3.rb

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ module Open3
8686
# Open3.popen3([env, ] command_line, options = {}) {|stdin, stdout, stderr, wait_thread| ... } -> object
8787
# Open3.popen3([env, ] exe_path, *args, options = {}) {|stdin, stdout, stderr, wait_thread| ... } -> object
8888
#
89-
# Basically a wrapper for Process.spawn that:
89+
# Basically a wrapper for
90+
# {Process.spawn}[https://docs.ruby-lang.org/en/master/Process.html#method-c-spawn]
91+
# that:
9092
#
9193
# - Creates a child process, by calling Process.spawn with the given arguments.
9294
# - Creates streams +stdin+, +stdout+, and +stderr+,
@@ -235,7 +237,9 @@ def popen3(*cmd, &block)
235237
# Open3.popen2([env, ] command_line, options = {}) {|stdin, stdout, wait_thread| ... } -> object
236238
# Open3.popen2([env, ] exe_path, *args, options = {}) {|stdin, stdout, wait_thread| ... } -> object
237239
#
238-
# Basically a wrapper for Process.spawn that:
240+
# Basically a wrapper for
241+
# {Process.spawn}[https://docs.ruby-lang.org/en/master/Process.html#method-c-spawn]
242+
# that:
239243
#
240244
# - Creates a child process, by calling Process.spawn with the given arguments.
241245
# - Creates streams +stdin+ and +stdout+,
@@ -372,7 +376,9 @@ def popen2(*cmd, &block)
372376
# Open3.popen2e([env, ] command_line, options = {}) {|stdin, stdout_and_stderr, wait_thread| ... } -> object
373377
# Open3.popen2e([env, ] exe_path, *args, options = {}) {|stdin, stdout_and_stderr, wait_thread| ... } -> object
374378
#
375-
# Basically a wrapper for Process.spawn that:
379+
# Basically a wrapper for
380+
# {Process.spawn}[https://docs.ruby-lang.org/en/master/Process.html#method-c-spawn]
381+
# that:
376382
#
377383
# - Creates a child process, by calling Process.spawn with the given arguments.
378384
# - Creates streams +stdin+, +stdout_and_stderr+,
@@ -953,43 +959,62 @@ def pipeline_rw(*cmds, &block)
953959
end
954960
module_function :pipeline_rw
955961

956-
# Open3.pipeline_r starts a list of commands as a pipeline with a pipe
957-
# which connects to stdout of the last command.
958-
#
959-
# Open3.pipeline_r(cmd1, cmd2, ... [, opts]) {|last_stdout, wait_threads|
960-
# ...
961-
# }
962+
# :call-seq:
963+
# Open3.pipeline_r([env, ] *cmds, options = {}) -> [last_stdout, wait_threads]
962964
#
963-
# last_stdout, wait_threads = Open3.pipeline_r(cmd1, cmd2, ... [, opts])
964-
# ...
965-
# last_stdout.close
965+
# Basically a wrapper for
966+
# {Process.spawn}[https://docs.ruby-lang.org/en/master/Process.html#method-c-spawn]
967+
# that:
966968
#
967-
# Each cmd is a string or an array.
968-
# If it is an array, the elements are passed to Process.spawn.
969+
# - Creates a child process for each of the given +cmds+
970+
# by calling Process.spawn.
971+
# - Pipes the +stdout+ from each child to the +stdin+ of the next child,
972+
# or, for the last child, to the caller's +stdout+.
973+
# - Waits for all child processes to exit.
969974
#
970-
# cmd:
971-
# commandline command line string which is passed to a shell
972-
# [env, commandline, opts] command line string which is passed to a shell
973-
# [env, cmdname, arg1, ..., opts] command name and one or more arguments (no shell)
974-
# [env, [cmdname, argv0], arg1, ..., opts] command name and arguments including argv[0] (no shell)
975+
# With no block given, returns a 2-element array containing:
975976
#
976-
# Note that env and opts are optional, as for Process.spawn.
977+
# - The +stdout+ stream of the last child process.
978+
# - An array of the wait threads for all of the child processes.
977979
#
978980
# Example:
979981
#
980-
# Open3.pipeline_r("zcat /var/log/apache2/access.log.*.gz",
981-
# [{"LANG"=>"C"}, "grep", "GET /favicon.ico"],
982-
# "logresolve") {|o, ts|
983-
# o.each_line {|line|
984-
# ...
985-
# }
986-
# }
982+
# Open3.pipeline_r('ls', 'grep R')
983+
# # => [#<IO:fd 5>, [#<Process::Waiter:0x00005638280167b8 sleep>, #<Process::Waiter:0x0000563828015480 dead>]]
987984
#
988-
# Open3.pipeline_r("yes", "head -10") {|o, ts|
989-
# p o.read #=> "y\ny\ny\ny\ny\ny\ny\ny\ny\ny\n"
990-
# p ts[0].value #=> #<Process::Status: pid 24910 SIGPIPE (signal 13)>
991-
# p ts[1].value #=> #<Process::Status: pid 24913 exit 0>
992-
# }
985+
# With a block given, calls the block with the +stdout+ stream
986+
# of the last child process:
987+
#
988+
# Open3.pipeline_r('ls', 'grep R') {|x| puts x.read }
989+
#
990+
# Output:
991+
#
992+
# CONTRIBUTING.md
993+
# Rakefile
994+
# README.md
995+
#
996+
# Like Process.spawn, this method has potential security vulnerabilities
997+
# if called with untrusted input;
998+
# see {Command Injection}[rdoc-ref:command_injection.rdoc].
999+
#
1000+
# Unlike Process.spawn, this method waits for the child processes to exit
1001+
# before returning, so the caller need not do so.
1002+
#
1003+
# If the first argument is a hash, it becomes leading argument +env+
1004+
# in each call to Process.spawn;
1005+
# see {Execution Environment}[https://docs.ruby-lang.org/en/master/Process.html#module-Process-label-Execution+Environment].
1006+
#
1007+
# If the last argument is a hash, it becomes trailing argument +options+
1008+
# in each call to Process.spawn;
1009+
# see {Execution Options}[https://docs.ruby-lang.org/en/master/Process.html#module-Process-label-Execution+Options].
1010+
#
1011+
# Each remaining argument in +cmds+ is one of:
1012+
#
1013+
# - A +command_line+: a string that begins with a shell reserved word
1014+
# or special built-in, or contains one or more metacharacters.
1015+
# - An +exe_path+: the string path to an executable to be called.
1016+
# - An array containing a +command_line+ or an +exe_path+,
1017+
# along with zero or more string arguments for the command.
9931018
#
9941019
def pipeline_r(*cmds, &block)
9951020
if Hash === cmds.last

0 commit comments

Comments
 (0)