Skip to content

Commit 4c9e749

Browse files
[DOC] RDoc for Open3 (#20)
1 parent 577bee9 commit 4c9e749

File tree

1 file changed

+66
-23
lines changed

1 file changed

+66
-23
lines changed

lib/open3.rb

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -983,15 +983,19 @@ def pipeline_rw(*cmds, &block)
983983
# # => [#<IO:fd 5>, [#<Process::Waiter:0x00005638280167b8 sleep>, #<Process::Waiter:0x0000563828015480 dead>]]
984984
#
985985
# With a block given, calls the block with the +stdout+ stream
986-
# of the last child process:
986+
# of the last child process,
987+
# and an array of the wait processes:
987988
#
988-
# Open3.pipeline_r('ls', 'grep R') {|x| puts x.read }
989+
# Open3.pipeline_r('ls', 'grep R') do |x, ts|
990+
# puts x.read
991+
# p ts
992+
# end
989993
#
990994
# Output:
991995
#
992-
# CONTRIBUTING.md
993996
# Rakefile
994997
# README.md
998+
# [#<Process::Waiter:0x000055f1d78d76f0 sleep>, #<Process::Waiter:0x000055f1d78d7358 dead>]
995999
#
9961000
# Like Process.spawn, this method has potential security vulnerabilities
9971001
# if called with untrusted input;
@@ -1030,33 +1034,72 @@ def pipeline_r(*cmds, &block)
10301034
end
10311035
module_function :pipeline_r
10321036

1033-
# Open3.pipeline_w starts a list of commands as a pipeline with a pipe
1034-
# which connects to stdin of the first command.
1035-
#
1036-
# Open3.pipeline_w(cmd1, cmd2, ... [, opts]) {|first_stdin, wait_threads|
1037-
# ...
1038-
# }
1037+
1038+
# :call-seq:
1039+
# Open3.pipeline_w([env, ] *cmds, options = {}) -> [first_stdin, wait_threads]
10391040
#
1040-
# first_stdin, wait_threads = Open3.pipeline_w(cmd1, cmd2, ... [, opts])
1041-
# ...
1042-
# first_stdin.close
1041+
# Basically a wrapper for
1042+
# {Process.spawn}[https://docs.ruby-lang.org/en/master/Process.html#method-c-spawn]
1043+
# that:
10431044
#
1044-
# Each cmd is a string or an array.
1045-
# If it is an array, the elements are passed to Process.spawn.
1045+
# - Creates a child process for each of the given +cmds+
1046+
# by calling Process.spawn.
1047+
# - Pipes the +stdout+ from each child to the +stdin+ of the next child,
1048+
# or, for the first child, pipes the caller's +stdout+ to the child's +stdin+.
1049+
# - Waits for all child processes to exit.
10461050
#
1047-
# cmd:
1048-
# commandline command line string which is passed to a shell
1049-
# [env, commandline, opts] command line string which is passed to a shell
1050-
# [env, cmdname, arg1, ..., opts] command name and one or more arguments (no shell)
1051-
# [env, [cmdname, argv0], arg1, ..., opts] command name and arguments including argv[0] (no shell)
1051+
# With no block given, returns a 2-element array containing:
10521052
#
1053-
# Note that env and opts are optional, as for Process.spawn.
1053+
# - The +stdin+ stream of the first child process.
1054+
# - An array of the wait threads for all of the child processes.
10541055
#
10551056
# Example:
10561057
#
1057-
# Open3.pipeline_w("bzip2 -c", :out=>"/tmp/hello.bz2") {|i, ts|
1058-
# i.puts "hello"
1059-
# }
1058+
# p Open3.pipeline_r(
1059+
# ['ruby', '-e', 'print "Foo"'],
1060+
# ['ruby', '-e', 'print STDIN.read + "Bar"']
1061+
# )
1062+
# [#<IO:fd 5>, [#<Process::Waiter:0x00005568cad44a08 sleep>, #<Process::Waiter:0x00005568cad44508 run>]]
1063+
#
1064+
# With a block given, calls the block with the +stdin+ stream
1065+
# of the first child process,
1066+
# and an array of the wait processes:
1067+
#
1068+
# Open3.pipeline_r(
1069+
# ['ruby', '-e', 'print "Foo"'],
1070+
# ['ruby', '-e', 'print STDIN.read + "Bar"']
1071+
# ) do |x, ts|
1072+
# puts x.read
1073+
# p ts
1074+
# end
1075+
#
1076+
# Output:
1077+
#
1078+
# FooBar
1079+
# [#<Process::Waiter:0x000055628e2ebbc0 dead>, #<Process::Waiter:0x000055628e2eb7b0 sleep>]
1080+
#
1081+
# Like Process.spawn, this method has potential security vulnerabilities
1082+
# if called with untrusted input;
1083+
# see {Command Injection}[rdoc-ref:command_injection.rdoc].
1084+
#
1085+
# Unlike Process.spawn, this method waits for the child processes to exit
1086+
# before returning, so the caller need not do so.
1087+
#
1088+
# If the first argument is a hash, it becomes leading argument +env+
1089+
# in each call to Process.spawn;
1090+
# see {Execution Environment}[https://docs.ruby-lang.org/en/master/Process.html#module-Process-label-Execution+Environment].
1091+
#
1092+
# If the last argument is a hash, it becomes trailing argument +options+
1093+
# in each call to Process.spawn;
1094+
# see {Execution Options}[https://docs.ruby-lang.org/en/master/Process.html#module-Process-label-Execution+Options].
1095+
#
1096+
# Each remaining argument in +cmds+ is one of:
1097+
#
1098+
# - A +command_line+: a string that begins with a shell reserved word
1099+
# or special built-in, or contains one or more metacharacters.
1100+
# - An +exe_path+: the string path to an executable to be called.
1101+
# - An array containing a +command_line+ or an +exe_path+,
1102+
# along with zero or more string arguments for the command.
10601103
#
10611104
def pipeline_w(*cmds, &block)
10621105
if Hash === cmds.last

0 commit comments

Comments
 (0)