Allows the opening of various resources including URIs.
If the first argument responds to the ‘open’ method, ‘open’ is called on it with the rest of the arguments.
If the first argument is a string that begins with (protocol)://
, it is parsed by URI.parse
. If the parsed object responds to the ‘open’ method, ‘open’ is called on it with the rest of the arguments.
Otherwise, Kernel#open
is called.
OpenURI::OpenRead#open
provides URI::HTTP#open
, URI::HTTPS#open
and URI::FTP#open
, Kernel#open
.
We can accept URIs and strings that begin with http://, https:// and ftp://. In these cases, the opened file object is extended by OpenURI::Meta
.
Basically a wrapper for Process.spawn
that:
Creates a child process, by calling Process.spawn
with the given arguments.
Creates streams stdin
, stdout
, and stderr
, which are the standard input, standard output, and standard error streams in the child process.
Creates thread wait_thread
that waits for the child process to exit; the thread has method pid
, which returns the process ID of the child process.
With no block given, returns the array [stdin, stdout, stderr, wait_thread]
. The caller should close each of the three returned streams.
stdin, stdout, stderr, wait_thread = Open3.popen3('echo') # => [#<IO:fd 8>, #<IO:fd 10>, #<IO:fd 12>, #<Process::Waiter:0x00007f58d5428f58 run>] stdin.close stdout.close stderr.close wait_thread.pid # => 2210481 wait_thread.value # => #<Process::Status: pid 2210481 exit 0>
With a block given, calls the block with the four variables (three streams and the wait thread) and returns the block’s return value. The caller need not close the streams:
Open3.popen3('echo') do |stdin, stdout, stderr, wait_thread| p stdin p stdout p stderr p wait_thread p wait_thread.pid p wait_thread.value end
Output:
#<IO:fd 6> #<IO:fd 7> #<IO:fd 9> #<Process::Waiter:0x00007f58d53606e8 sleep> 2211047 #<Process::Status: pid 2211047 exit 0>
Like Process.spawn
, this method has potential security vulnerabilities if called with untrusted input; see Command Injection.
Unlike Process.spawn
, this method waits for the child process to exit before returning, so the caller need not do so.
If the first argument is a hash, it becomes leading argument env
in the call to Process.spawn
; see Execution Environment.
If the last argument is a hash, it becomes trailing argument options
in the call to Process.spawn
; see Execution Options.
The single required argument is one of the following:
command_line
if it is a string, and if it begins with a shell reserved word or special built-in, or if it contains one or more metacharacters.
exe_path
otherwise.
Argument command_line
String argument command_line
is a command line to be passed to a shell; it must begin with a shell reserved word, begin with a special built-in, or contain meta characters:
Open3.popen3('if true; then echo "Foo"; fi') {|*args| p args } # Shell reserved word. Open3.popen3('echo') {|*args| p args } # Built-in. Open3.popen3('date > date.tmp') {|*args| p args } # Contains meta character.
Output (similar for each call above):
[#<IO:(closed)>, #<IO:(closed)>, #<IO:(closed)>, #<Process::Waiter:0x00007f58d52f28c8 dead>]
The command line may also contain arguments and options for the command:
Open3.popen3('echo "Foo"') { |i, o, e, t| o.gets } "Foo\n"
Argument exe_path
Argument exe_path
is one of the following:
The string path to an executable to be called.
A 2-element array containing the path to an executable and the string to be used as the name of the executing process.
Example:
Open3.popen3('/usr/bin/date') { |i, o, e, t| o.gets } # => "Wed Sep 27 02:56:44 PM CDT 2023\n"
Ruby
invokes the executable directly, with no shell and no shell expansion:
Open3.popen3('doesnt_exist') { |i, o, e, t| o.gets } # Raises Errno::ENOENT
If one or more args
is given, each is an argument or option to be passed to the executable:
Open3.popen3('echo', 'C #') { |i, o, e, t| o.gets } # => "C #\n" Open3.popen3('echo', 'hello', 'world') { |i, o, e, t| o.gets } # => "hello world\n"
Take care to avoid deadlocks. Output streams stdout
and stderr
have fixed-size buffers, so reading extensively from one but not the other can cause a deadlock when the unread buffer fills. To avoid that, stdout
and stderr
should be read simultaneously (using threads or IO.select
).
Related:
Open3.popen2
: Makes the standard input and standard output streams of the child process available as separate streams, with no access to the standard error stream.
Open3.popen2e
: Makes the standard input and the merge of the standard output and standard error streams of the child process available as separate streams.
Basically a wrapper for Process.spawn
that:
Creates a child process, by calling Process.spawn
with the given arguments.
Creates streams stdin
, stdout
, and stderr
, which are the standard input, standard output, and standard error streams in the child process.
Creates thread wait_thread
that waits for the child process to exit; the thread has method pid
, which returns the process ID of the child process.
With no block given, returns the array [stdin, stdout, stderr, wait_thread]
. The caller should close each of the three returned streams.
stdin, stdout, stderr, wait_thread = Open3.popen3('echo') # => [#<IO:fd 8>, #<IO:fd 10>, #<IO:fd 12>, #<Process::Waiter:0x00007f58d5428f58 run>] stdin.close stdout.close stderr.close wait_thread.pid # => 2210481 wait_thread.value # => #<Process::Status: pid 2210481 exit 0>
With a block given, calls the block with the four variables (three streams and the wait thread) and returns the block’s return value. The caller need not close the streams:
Open3.popen3('echo') do |stdin, stdout, stderr, wait_thread| p stdin p stdout p stderr p wait_thread p wait_thread.pid p wait_thread.value end
Output:
#<IO:fd 6> #<IO:fd 7> #<IO:fd 9> #<Process::Waiter:0x00007f58d53606e8 sleep> 2211047 #<Process::Status: pid 2211047 exit 0>
Like Process.spawn
, this method has potential security vulnerabilities if called with untrusted input; see Command Injection.
Unlike Process.spawn
, this method waits for the child process to exit before returning, so the caller need not do so.
If the first argument is a hash, it becomes leading argument env
in the call to Process.spawn
; see Execution Environment.
If the last argument is a hash, it becomes trailing argument options
in the call to Process.spawn
; see Execution Options.
The single required argument is one of the following:
command_line
if it is a string, and if it begins with a shell reserved word or special built-in, or if it contains one or more metacharacters.
exe_path
otherwise.
Argument command_line
String argument command_line
is a command line to be passed to a shell; it must begin with a shell reserved word, begin with a special built-in, or contain meta characters:
Open3.popen3('if true; then echo "Foo"; fi') {|*args| p args } # Shell reserved word. Open3.popen3('echo') {|*args| p args } # Built-in. Open3.popen3('date > date.tmp') {|*args| p args } # Contains meta character.
Output (similar for each call above):
[#<IO:(closed)>, #<IO:(closed)>, #<IO:(closed)>, #<Process::Waiter:0x00007f58d52f28c8 dead>]
The command line may also contain arguments and options for the command:
Open3.popen3('echo "Foo"') { |i, o, e, t| o.gets } "Foo\n"
Argument exe_path
Argument exe_path
is one of the following:
The string path to an executable to be called.
A 2-element array containing the path to an executable and the string to be used as the name of the executing process.
Example:
Open3.popen3('/usr/bin/date') { |i, o, e, t| o.gets } # => "Wed Sep 27 02:56:44 PM CDT 2023\n"
Ruby
invokes the executable directly, with no shell and no shell expansion:
Open3.popen3('doesnt_exist') { |i, o, e, t| o.gets } # Raises Errno::ENOENT
If one or more args
is given, each is an argument or option to be passed to the executable:
Open3.popen3('echo', 'C #') { |i, o, e, t| o.gets } # => "C #\n" Open3.popen3('echo', 'hello', 'world') { |i, o, e, t| o.gets } # => "hello world\n"
Take care to avoid deadlocks. Output streams stdout
and stderr
have fixed-size buffers, so reading extensively from one but not the other can cause a deadlock when the unread buffer fills. To avoid that, stdout
and stderr
should be read simultaneously (using threads or IO.select
).
Related:
Open3.popen2
: Makes the standard input and standard output streams of the child process available as separate streams, with no access to the standard error stream.
Open3.popen2e
: Makes the standard input and the merge of the standard output and standard error streams of the child process available as separate streams.
Basically a wrapper for Process.spawn
that:
Creates a child process, by calling Process.spawn
with the given arguments.
Creates streams stdin
and stdout
, which are the standard input and standard output streams in the child process.
Creates thread wait_thread
that waits for the child process to exit; the thread has method pid
, which returns the process ID of the child process.
With no block given, returns the array [stdin, stdout, wait_thread]
. The caller should close each of the two returned streams.
stdin, stdout, wait_thread = Open3.popen2('echo') # => [#<IO:fd 6>, #<IO:fd 7>, #<Process::Waiter:0x00007f58d52dbe98 run>] stdin.close stdout.close wait_thread.pid # => 2263572 wait_thread.value # => #<Process::Status: pid 2263572 exit 0>
With a block given, calls the block with the three variables (two streams and the wait thread) and returns the block’s return value. The caller need not close the streams:
Open3.popen2('echo') do |stdin, stdout, wait_thread| p stdin p stdout p wait_thread p wait_thread.pid p wait_thread.value end
Output:
#<IO:fd 6> #<IO:fd 7> #<Process::Waiter:0x00007f58d59a34b0 sleep> 2263636 #<Process::Status: pid 2263636 exit 0>
Like Process.spawn
, this method has potential security vulnerabilities if called with untrusted input; see Command Injection.
Unlike Process.spawn
, this method waits for the child process to exit before returning, so the caller need not do so.
If the first argument is a hash, it becomes leading argument env
in the call to Process.spawn
; see Execution Environment.
If the last argument is a hash, it becomes trailing argument options
in the call to Process.spawn
; see Execution Options.
The single required argument is one of the following:
command_line
if it is a string, and if it begins with a shell reserved word or special built-in, or if it contains one or more metacharacters.
exe_path
otherwise.
Argument command_line
String argument command_line
is a command line to be passed to a shell; it must begin with a shell reserved word, begin with a special built-in, or contain meta characters:
Open3.popen2('if true; then echo "Foo"; fi') {|*args| p args } # Shell reserved word. Open3.popen2('echo') {|*args| p args } # Built-in. Open3.popen2('date > date.tmp') {|*args| p args } # Contains meta character.
Output (similar for each call above):
# => [#<IO:(closed)>, #<IO:(closed)>, #<Process::Waiter:0x00007f7577dfe410 dead>]
The command line may also contain arguments and options for the command:
Open3.popen2('echo "Foo"') { |i, o, t| o.gets } "Foo\n"
Argument exe_path
Argument exe_path
is one of the following:
The string path to an executable to be called.
A 2-element array containing the path to an executable and the string to be used as the name of the executing process.
Example:
Open3.popen2('/usr/bin/date') { |i, o, t| o.gets } # => "Thu Sep 28 09:41:06 AM CDT 2023\n"
Ruby
invokes the executable directly, with no shell and no shell expansion:
Open3.popen2('doesnt_exist') { |i, o, t| o.gets } # Raises Errno::ENOENT
If one or more args
is given, each is an argument or option to be passed to the executable:
Open3.popen2('echo', 'C #') { |i, o, t| o.gets } # => "C #\n" Open3.popen2('echo', 'hello', 'world') { |i, o, t| o.gets } # => "hello world\n"
Related:
Open3.popen2e
: Makes the standard input and the merge of the standard output and standard error streams of the child process available as separate streams.
Open3.popen3
: Makes the standard input, standard output, and standard error streams of the child process available as separate streams.
Basically a wrapper for Process.spawn
that:
Creates a child process, by calling Process.spawn
with the given arguments.
Creates streams stdin
and stdout
, which are the standard input and standard output streams in the child process.
Creates thread wait_thread
that waits for the child process to exit; the thread has method pid
, which returns the process ID of the child process.
With no block given, returns the array [stdin, stdout, wait_thread]
. The caller should close each of the two returned streams.
stdin, stdout, wait_thread = Open3.popen2('echo') # => [#<IO:fd 6>, #<IO:fd 7>, #<Process::Waiter:0x00007f58d52dbe98 run>] stdin.close stdout.close wait_thread.pid # => 2263572 wait_thread.value # => #<Process::Status: pid 2263572 exit 0>
With a block given, calls the block with the three variables (two streams and the wait thread) and returns the block’s return value. The caller need not close the streams:
Open3.popen2('echo') do |stdin, stdout, wait_thread| p stdin p stdout p wait_thread p wait_thread.pid p wait_thread.value end
Output:
#<IO:fd 6> #<IO:fd 7> #<Process::Waiter:0x00007f58d59a34b0 sleep> 2263636 #<Process::Status: pid 2263636 exit 0>
Like Process.spawn
, this method has potential security vulnerabilities if called with untrusted input; see Command Injection.
Unlike Process.spawn
, this method waits for the child process to exit before returning, so the caller need not do so.
If the first argument is a hash, it becomes leading argument env
in the call to Process.spawn
; see Execution Environment.
If the last argument is a hash, it becomes trailing argument options
in the call to Process.spawn
; see Execution Options.
The single required argument is one of the following:
command_line
if it is a string, and if it begins with a shell reserved word or special built-in, or if it contains one or more metacharacters.
exe_path
otherwise.
Argument command_line
String argument command_line
is a command line to be passed to a shell; it must begin with a shell reserved word, begin with a special built-in, or contain meta characters:
Open3.popen2('if true; then echo "Foo"; fi') {|*args| p args } # Shell reserved word. Open3.popen2('echo') {|*args| p args } # Built-in. Open3.popen2('date > date.tmp') {|*args| p args } # Contains meta character.
Output (similar for each call above):
# => [#<IO:(closed)>, #<IO:(closed)>, #<Process::Waiter:0x00007f7577dfe410 dead>]
The command line may also contain arguments and options for the command:
Open3.popen2('echo "Foo"') { |i, o, t| o.gets } "Foo\n"
Argument exe_path
Argument exe_path
is one of the following:
The string path to an executable to be called.
A 2-element array containing the path to an executable and the string to be used as the name of the executing process.
Example:
Open3.popen2('/usr/bin/date') { |i, o, t| o.gets } # => "Thu Sep 28 09:41:06 AM CDT 2023\n"
Ruby
invokes the executable directly, with no shell and no shell expansion:
Open3.popen2('doesnt_exist') { |i, o, t| o.gets } # Raises Errno::ENOENT
If one or more args
is given, each is an argument or option to be passed to the executable:
Open3.popen2('echo', 'C #') { |i, o, t| o.gets } # => "C #\n" Open3.popen2('echo', 'hello', 'world') { |i, o, t| o.gets } # => "hello world\n"
Related:
Open3.popen2e
: Makes the standard input and the merge of the standard output and standard error streams of the child process available as separate streams.
Open3.popen3
: Makes the standard input, standard output, and standard error streams of the child process available as separate streams.
Basically a wrapper for Process.spawn
that:
Creates a child process, by calling Process.spawn
with the given arguments.
Creates streams stdin
, stdout_and_stderr
, which are the standard input and the merge of the standard output and standard error streams in the child process.
Creates thread wait_thread
that waits for the child process to exit; the thread has method pid
, which returns the process ID of the child process.
With no block given, returns the array [stdin, stdout_and_stderr, wait_thread]
. The caller should close each of the two returned streams.
stdin, stdout_and_stderr, wait_thread = Open3.popen2e('echo') # => [#<IO:fd 6>, #<IO:fd 7>, #<Process::Waiter:0x00007f7577da4398 run>] stdin.close stdout_and_stderr.close wait_thread.pid # => 2274600 wait_thread.value # => #<Process::Status: pid 2274600 exit 0>
With a block given, calls the block with the three variables (two streams and the wait thread) and returns the block’s return value. The caller need not close the streams:
Open3.popen2e('echo') do |stdin, stdout_and_stderr, wait_thread| p stdin p stdout_and_stderr p wait_thread p wait_thread.pid p wait_thread.value end
Output:
#<IO:fd 6> #<IO:fd 7> #<Process::Waiter:0x00007f75777578c8 sleep> 2274763 #<Process::Status: pid 2274763 exit 0>
Like Process.spawn
, this method has potential security vulnerabilities if called with untrusted input; see Command Injection.
Unlike Process.spawn
, this method waits for the child process to exit before returning, so the caller need not do so.
If the first argument is a hash, it becomes leading argument env
in the call to Process.spawn
; see Execution Environment.
If the last argument is a hash, it becomes trailing argument options
in the call to Process.spawn
; see Execution Options.
The single required argument is one of the following:
command_line
if it is a string, and if it begins with a shell reserved word or special built-in, or if it contains one or more metacharacters.
exe_path
otherwise.
Argument command_line
String argument command_line
is a command line to be passed to a shell; it must begin with a shell reserved word, begin with a special built-in, or contain meta characters:
Open3.popen2e('if true; then echo "Foo"; fi') {|*args| p args } # Shell reserved word. Open3.popen2e('echo') {|*args| p args } # Built-in. Open3.popen2e('date > date.tmp') {|*args| p args } # Contains meta character.
Output (similar for each call above):
# => [#<IO:(closed)>, #<IO:(closed)>, #<Process::Waiter:0x00007f7577d8a1f0 dead>]
The command line may also contain arguments and options for the command:
Open3.popen2e('echo "Foo"') { |i, o_and_e, t| o_and_e.gets } "Foo\n"
Argument exe_path
Argument exe_path
is one of the following:
The string path to an executable to be called.
A 2-element array containing the path to an executable and the string to be used as the name of the executing process.
Example:
Open3.popen2e('/usr/bin/date') { |i, o_and_e, t| o_and_e.gets } # => "Thu Sep 28 01:58:45 PM CDT 2023\n"
Ruby
invokes the executable directly, with no shell and no shell expansion:
Open3.popen2e('doesnt_exist') { |i, o_and_e, t| o_and_e.gets } # Raises Errno::ENOENT
If one or more args
is given, each is an argument or option to be passed to the executable:
Open3.popen2e('echo', 'C #') { |i, o_and_e, t| o_and_e.gets } # => "C #\n" Open3.popen2e('echo', 'hello', 'world') { |i, o_and_e, t| o_and_e.gets } # => "hello world\n"
Related:
Open3.popen2
: Makes the standard input and standard output streams of the child process available as separate streams, with no access to the standard error stream.
Open3.popen3
: Makes the standard input, standard output, and standard error streams of the child process available as separate streams.
Basically a wrapper for Process.spawn
that:
Creates a child process, by calling Process.spawn
with the given arguments.
Creates streams stdin
, stdout_and_stderr
, which are the standard input and the merge of the standard output and standard error streams in the child process.
Creates thread wait_thread
that waits for the child process to exit; the thread has method pid
, which returns the process ID of the child process.
With no block given, returns the array [stdin, stdout_and_stderr, wait_thread]
. The caller should close each of the two returned streams.
stdin, stdout_and_stderr, wait_thread = Open3.popen2e('echo') # => [#<IO:fd 6>, #<IO:fd 7>, #<Process::Waiter:0x00007f7577da4398 run>] stdin.close stdout_and_stderr.close wait_thread.pid # => 2274600 wait_thread.value # => #<Process::Status: pid 2274600 exit 0>
With a block given, calls the block with the three variables (two streams and the wait thread) and returns the block’s return value. The caller need not close the streams:
Open3.popen2e('echo') do |stdin, stdout_and_stderr, wait_thread| p stdin p stdout_and_stderr p wait_thread p wait_thread.pid p wait_thread.value end
Output:
#<IO:fd 6> #<IO:fd 7> #<Process::Waiter:0x00007f75777578c8 sleep> 2274763 #<Process::Status: pid 2274763 exit 0>
Like Process.spawn
, this method has potential security vulnerabilities if called with untrusted input; see Command Injection.
Unlike Process.spawn
, this method waits for the child process to exit before returning, so the caller need not do so.
If the first argument is a hash, it becomes leading argument env
in the call to Process.spawn
; see Execution Environment.
If the last argument is a hash, it becomes trailing argument options
in the call to Process.spawn
; see Execution Options.
The single required argument is one of the following:
command_line
if it is a string, and if it begins with a shell reserved word or special built-in, or if it contains one or more metacharacters.
exe_path
otherwise.
Argument command_line
String argument command_line
is a command line to be passed to a shell; it must begin with a shell reserved word, begin with a special built-in, or contain meta characters:
Open3.popen2e('if true; then echo "Foo"; fi') {|*args| p args } # Shell reserved word. Open3.popen2e('echo') {|*args| p args } # Built-in. Open3.popen2e('date > date.tmp') {|*args| p args } # Contains meta character.
Output (similar for each call above):
# => [#<IO:(closed)>, #<IO:(closed)>, #<Process::Waiter:0x00007f7577d8a1f0 dead>]
The command line may also contain arguments and options for the command:
Open3.popen2e('echo "Foo"') { |i, o_and_e, t| o_and_e.gets } "Foo\n"
Argument exe_path
Argument exe_path
is one of the following:
The string path to an executable to be called.
A 2-element array containing the path to an executable and the string to be used as the name of the executing process.
Example:
Open3.popen2e('/usr/bin/date') { |i, o_and_e, t| o_and_e.gets } # => "Thu Sep 28 01:58:45 PM CDT 2023\n"
Ruby
invokes the executable directly, with no shell and no shell expansion:
Open3.popen2e('doesnt_exist') { |i, o_and_e, t| o_and_e.gets } # Raises Errno::ENOENT
If one or more args
is given, each is an argument or option to be passed to the executable:
Open3.popen2e('echo', 'C #') { |i, o_and_e, t| o_and_e.gets } # => "C #\n" Open3.popen2e('echo', 'hello', 'world') { |i, o_and_e, t| o_and_e.gets } # => "hello world\n"
Related:
Open3.popen2
: Makes the standard input and standard output streams of the child process available as separate streams, with no access to the standard error stream.
Open3.popen3
: Makes the standard input, standard output, and standard error streams of the child process available as separate streams.
Basically a wrapper for Open3.popen3
that:
Creates a child process, by calling Open3.popen3
with the given arguments (except for certain entries in hash options
; see below).
Returns as strings stdout_s
and stderr_s
the standard output and standard error of the child process.
Returns as status
a Process::Status
object that represents the exit status of the child process.
Returns the array [stdout_s, stderr_s, status]
:
stdout_s, stderr_s, status = Open3.capture3('echo "Foo"') # => ["Foo\n", "", #<Process::Status: pid 2281954 exit 0>]
Like Process.spawn
, this method has potential security vulnerabilities if called with untrusted input; see Command Injection.
Unlike Process.spawn
, this method waits for the child process to exit before returning, so the caller need not do so.
If the first argument is a hash, it becomes leading argument env
in the call to Open3.popen3
; see Execution Environment.
If the last argument is a hash, it becomes trailing argument options
in the call to Open3.popen3
; see Execution Options.
The hash options
is given; two options have local effect in method Open3.capture3
:
If entry options[:stdin_data]
exists, the entry is removed and its string value is sent to the command’s standard input:
Open3.capture3('tee', stdin_data: 'Foo') # => ["Foo", "", #<Process::Status: pid 2319575 exit 0>]
If entry options[:binmode]
exists, the entry is removed and the internal streams are set to binary mode.
The single required argument is one of the following:
command_line
if it is a string, and if it begins with a shell reserved word or special built-in, or if it contains one or more metacharacters.
exe_path
otherwise.
Argument command_line
String argument command_line
is a command line to be passed to a shell; it must begin with a shell reserved word, begin with a special built-in, or contain meta characters:
Open3.capture3('if true; then echo "Foo"; fi') # Shell reserved word. # => ["Foo\n", "", #<Process::Status: pid 2282025 exit 0>] Open3.capture3('echo') # Built-in. # => ["\n", "", #<Process::Status: pid 2282092 exit 0>] Open3.capture3('date > date.tmp') # Contains meta character. # => ["", "", #<Process::Status: pid 2282110 exit 0>]
The command line may also contain arguments and options for the command:
Open3.capture3('echo "Foo"') # => ["Foo\n", "", #<Process::Status: pid 2282092 exit 0>]
Argument exe_path
Argument exe_path
is one of the following:
The string path to an executable to be called.
A 2-element array containing the path to an executable and the string to be used as the name of the executing process.
Example:
Open3.capture3('/usr/bin/date') # => ["Thu Sep 28 05:03:51 PM CDT 2023\n", "", #<Process::Status: pid 2282300 exit 0>]
Ruby
invokes the executable directly, with no shell and no shell expansion:
Open3.capture3('doesnt_exist') # Raises Errno::ENOENT
If one or more args
is given, each is an argument or option to be passed to the executable:
Open3.capture3('echo', 'C #') # => ["C #\n", "", #<Process::Status: pid 2282368 exit 0>] Open3.capture3('echo', 'hello', 'world') # => ["hello world\n", "", #<Process::Status: pid 2282372 exit 0>]
Basically a wrapper for Open3.popen3
that:
Creates a child process, by calling Open3.popen3
with the given arguments (except for certain entries in hash options
; see below).
Returns as strings stdout_s
and stderr_s
the standard output and standard error of the child process.
Returns as status
a Process::Status
object that represents the exit status of the child process.
Returns the array [stdout_s, stderr_s, status]
:
stdout_s, stderr_s, status = Open3.capture3('echo "Foo"') # => ["Foo\n", "", #<Process::Status: pid 2281954 exit 0>]
Like Process.spawn
, this method has potential security vulnerabilities if called with untrusted input; see Command Injection.
Unlike Process.spawn
, this method waits for the child process to exit before returning, so the caller need not do so.
If the first argument is a hash, it becomes leading argument env
in the call to Open3.popen3
; see Execution Environment.
If the last argument is a hash, it becomes trailing argument options
in the call to Open3.popen3
; see Execution Options.
The hash options
is given; two options have local effect in method Open3.capture3
:
If entry options[:stdin_data]
exists, the entry is removed and its string value is sent to the command’s standard input:
Open3.capture3('tee', stdin_data: 'Foo') # => ["Foo", "", #<Process::Status: pid 2319575 exit 0>]
If entry options[:binmode]
exists, the entry is removed and the internal streams are set to binary mode.
The single required argument is one of the following:
command_line
if it is a string, and if it begins with a shell reserved word or special built-in, or if it contains one or more metacharacters.
exe_path
otherwise.
Argument command_line
String argument command_line
is a command line to be passed to a shell; it must begin with a shell reserved word, begin with a special built-in, or contain meta characters:
Open3.capture3('if true; then echo "Foo"; fi') # Shell reserved word. # => ["Foo\n", "", #<Process::Status: pid 2282025 exit 0>] Open3.capture3('echo') # Built-in. # => ["\n", "", #<Process::Status: pid 2282092 exit 0>] Open3.capture3('date > date.tmp') # Contains meta character. # => ["", "", #<Process::Status: pid 2282110 exit 0>]
The command line may also contain arguments and options for the command:
Open3.capture3('echo "Foo"') # => ["Foo\n", "", #<Process::Status: pid 2282092 exit 0>]
Argument exe_path
Argument exe_path
is one of the following:
The string path to an executable to be called.
A 2-element array containing the path to an executable and the string to be used as the name of the executing process.
Example:
Open3.capture3('/usr/bin/date') # => ["Thu Sep 28 05:03:51 PM CDT 2023\n", "", #<Process::Status: pid 2282300 exit 0>]
Ruby
invokes the executable directly, with no shell and no shell expansion:
Open3.capture3('doesnt_exist') # Raises Errno::ENOENT
If one or more args
is given, each is an argument or option to be passed to the executable:
Open3.capture3('echo', 'C #') # => ["C #\n", "", #<Process::Status: pid 2282368 exit 0>] Open3.capture3('echo', 'hello', 'world') # => ["hello world\n", "", #<Process::Status: pid 2282372 exit 0>]
Basically a wrapper for Open3.popen3
that:
Creates a child process, by calling Open3.popen3
with the given arguments (except for certain entries in hash options
; see below).
Returns as string stdout_s
the standard output of the child process.
Returns as status
a Process::Status
object that represents the exit status of the child process.
Returns the array [stdout_s, status]
:
stdout_s, status = Open3.capture2('echo "Foo"') # => ["Foo\n", #<Process::Status: pid 2326047 exit 0>]
Like Process.spawn
, this method has potential security vulnerabilities if called with untrusted input; see Command Injection.
Unlike Process.spawn
, this method waits for the child process to exit before returning, so the caller need not do so.
If the first argument is a hash, it becomes leading argument env
in the call to Open3.popen3
; see Execution Environment.
If the last argument is a hash, it becomes trailing argument options
in the call to Open3.popen3
; see Execution Options.
The hash options
is given; two options have local effect in method Open3.capture2
:
If entry options[:stdin_data]
exists, the entry is removed and its string value is sent to the command’s standard input:
Open3.capture2('tee', stdin_data: 'Foo') # => ["Foo", #<Process::Status: pid 2326087 exit 0>]
If entry options[:binmode]
exists, the entry is removed and the internal streams are set to binary mode.
The single required argument is one of the following:
command_line
if it is a string, and if it begins with a shell reserved word or special built-in, or if it contains one or more metacharacters.
exe_path
otherwise.
Argument command_line
String argument command_line
is a command line to be passed to a shell; it must begin with a shell reserved word, begin with a special built-in, or contain meta characters:
Open3.capture2('if true; then echo "Foo"; fi') # Shell reserved word. # => ["Foo\n", #<Process::Status: pid 2326131 exit 0>] Open3.capture2('echo') # Built-in. # => ["\n", #<Process::Status: pid 2326139 exit 0>] Open3.capture2('date > date.tmp') # Contains meta character. # => ["", #<Process::Status: pid 2326174 exit 0>]
The command line may also contain arguments and options for the command:
Open3.capture2('echo "Foo"') # => ["Foo\n", #<Process::Status: pid 2326183 exit 0>]
Argument exe_path
Argument exe_path
is one of the following:
The string path to an executable to be called.
A 2-element array containing the path to an executable and the string to be used as the name of the executing process.
Example:
Open3.capture2('/usr/bin/date') # => ["Fri Sep 29 01:00:39 PM CDT 2023\n", #<Process::Status: pid 2326222 exit 0>]
Ruby
invokes the executable directly, with no shell and no shell expansion:
Open3.capture2('doesnt_exist') # Raises Errno::ENOENT
If one or more args
is given, each is an argument or option to be passed to the executable:
Open3.capture2('echo', 'C #') # => ["C #\n", #<Process::Status: pid 2326267 exit 0>] Open3.capture2('echo', 'hello', 'world') # => ["hello world\n", #<Process::Status: pid 2326299 exit 0>]
Basically a wrapper for Open3.popen3
that:
Creates a child process, by calling Open3.popen3
with the given arguments (except for certain entries in hash options
; see below).
Returns as string stdout_s
the standard output of the child process.
Returns as status
a Process::Status
object that represents the exit status of the child process.
Returns the array [stdout_s, status]
:
stdout_s, status = Open3.capture2('echo "Foo"') # => ["Foo\n", #<Process::Status: pid 2326047 exit 0>]
Like Process.spawn
, this method has potential security vulnerabilities if called with untrusted input; see Command Injection.
Unlike Process.spawn
, this method waits for the child process to exit before returning, so the caller need not do so.
If the first argument is a hash, it becomes leading argument env
in the call to Open3.popen3
; see Execution Environment.
If the last argument is a hash, it becomes trailing argument options
in the call to Open3.popen3
; see Execution Options.
The hash options
is given; two options have local effect in method Open3.capture2
:
If entry options[:stdin_data]
exists, the entry is removed and its string value is sent to the command’s standard input:
Open3.capture2('tee', stdin_data: 'Foo') # => ["Foo", #<Process::Status: pid 2326087 exit 0>]
If entry options[:binmode]
exists, the entry is removed and the internal streams are set to binary mode.
The single required argument is one of the following:
command_line
if it is a string, and if it begins with a shell reserved word or special built-in, or if it contains one or more metacharacters.
exe_path
otherwise.
Argument command_line
String argument command_line
is a command line to be passed to a shell; it must begin with a shell reserved word, begin with a special built-in, or contain meta characters:
Open3.capture2('if true; then echo "Foo"; fi') # Shell reserved word. # => ["Foo\n", #<Process::Status: pid 2326131 exit 0>] Open3.capture2('echo') # Built-in. # => ["\n", #<Process::Status: pid 2326139 exit 0>] Open3.capture2('date > date.tmp') # Contains meta character. # => ["", #<Process::Status: pid 2326174 exit 0>]
The command line may also contain arguments and options for the command:
Open3.capture2('echo "Foo"') # => ["Foo\n", #<Process::Status: pid 2326183 exit 0>]
Argument exe_path
Argument exe_path
is one of the following:
The string path to an executable to be called.
A 2-element array containing the path to an executable and the string to be used as the name of the executing process.
Example:
Open3.capture2('/usr/bin/date') # => ["Fri Sep 29 01:00:39 PM CDT 2023\n", #<Process::Status: pid 2326222 exit 0>]
Ruby
invokes the executable directly, with no shell and no shell expansion:
Open3.capture2('doesnt_exist') # Raises Errno::ENOENT
If one or more args
is given, each is an argument or option to be passed to the executable:
Open3.capture2('echo', 'C #') # => ["C #\n", #<Process::Status: pid 2326267 exit 0>] Open3.capture2('echo', 'hello', 'world') # => ["hello world\n", #<Process::Status: pid 2326299 exit 0>]
Basically a wrapper for Open3.popen3
that:
Creates a child process, by calling Open3.popen3
with the given arguments (except for certain entries in hash options
; see below).
Returns as string stdout_and_stderr_s
the merged standard output and standard error of the child process.
Returns as status
a Process::Status
object that represents the exit status of the child process.
Returns the array [stdout_and_stderr_s, status]
:
stdout_and_stderr_s, status = Open3.capture2e('echo "Foo"') # => ["Foo\n", #<Process::Status: pid 2371692 exit 0>]
Like Process.spawn
, this method has potential security vulnerabilities if called with untrusted input; see Command Injection.
Unlike Process.spawn
, this method waits for the child process to exit before returning, so the caller need not do so.
If the first argument is a hash, it becomes leading argument env
in the call to Open3.popen3
; see Execution Environment.
If the last argument is a hash, it becomes trailing argument options
in the call to Open3.popen3
; see Execution Options.
The hash options
is given; two options have local effect in method Open3.capture2e
:
If entry options[:stdin_data]
exists, the entry is removed and its string value is sent to the command’s standard input:
Open3.capture2e('tee', stdin_data: 'Foo') # => ["Foo", #<Process::Status: pid 2371732 exit 0>]
If entry options[:binmode]
exists, the entry is removed and the internal streams are set to binary mode.
The single required argument is one of the following:
command_line
if it is a string, and if it begins with a shell reserved word or special built-in, or if it contains one or more metacharacters.
exe_path
otherwise.
Argument command_line
String argument command_line
is a command line to be passed to a shell; it must begin with a shell reserved word, begin with a special built-in, or contain meta characters:
Open3.capture2e('if true; then echo "Foo"; fi') # Shell reserved word. # => ["Foo\n", #<Process::Status: pid 2371740 exit 0>] Open3.capture2e('echo') # Built-in. # => ["\n", #<Process::Status: pid 2371774 exit 0>] Open3.capture2e('date > date.tmp') # Contains meta character. # => ["", #<Process::Status: pid 2371812 exit 0>]
The command line may also contain arguments and options for the command:
Open3.capture2e('echo "Foo"') # => ["Foo\n", #<Process::Status: pid 2326183 exit 0>]
Argument exe_path
Argument exe_path
is one of the following:
The string path to an executable to be called.
A 2-element array containing the path to an executable and the string to be used as the name of the executing process.
Example:
Open3.capture2e('/usr/bin/date') # => ["Sat Sep 30 09:01:46 AM CDT 2023\n", #<Process::Status: pid 2371820 exit 0>]
Ruby
invokes the executable directly, with no shell and no shell expansion:
Open3.capture2e('doesnt_exist') # Raises Errno::ENOENT
If one or more args
is given, each is an argument or option to be passed to the executable:
Open3.capture2e('echo', 'C #') # => ["C #\n", #<Process::Status: pid 2371856 exit 0>] Open3.capture2e('echo', 'hello', 'world') # => ["hello world\n", #<Process::Status: pid 2371894 exit 0>]
Basically a wrapper for Open3.popen3
that:
Creates a child process, by calling Open3.popen3
with the given arguments (except for certain entries in hash options
; see below).
Returns as string stdout_and_stderr_s
the merged standard output and standard error of the child process.
Returns as status
a Process::Status
object that represents the exit status of the child process.
Returns the array [stdout_and_stderr_s, status]
:
stdout_and_stderr_s, status = Open3.capture2e('echo "Foo"') # => ["Foo\n", #<Process::Status: pid 2371692 exit 0>]
Like Process.spawn
, this method has potential security vulnerabilities if called with untrusted input; see Command Injection.
Unlike Process.spawn
, this method waits for the child process to exit before returning, so the caller need not do so.
If the first argument is a hash, it becomes leading argument env
in the call to Open3.popen3
; see Execution Environment.
If the last argument is a hash, it becomes trailing argument options
in the call to Open3.popen3
; see Execution Options.
The hash options
is given; two options have local effect in method Open3.capture2e
:
If entry options[:stdin_data]
exists, the entry is removed and its string value is sent to the command’s standard input:
Open3.capture2e('tee', stdin_data: 'Foo') # => ["Foo", #<Process::Status: pid 2371732 exit 0>]
If entry options[:binmode]
exists, the entry is removed and the internal streams are set to binary mode.
The single required argument is one of the following:
command_line
if it is a string, and if it begins with a shell reserved word or special built-in, or if it contains one or more metacharacters.
exe_path
otherwise.
Argument command_line
String argument command_line
is a command line to be passed to a shell; it must begin with a shell reserved word, begin with a special built-in, or contain meta characters:
Open3.capture2e('if true; then echo "Foo"; fi') # Shell reserved word. # => ["Foo\n", #<Process::Status: pid 2371740 exit 0>] Open3.capture2e('echo') # Built-in. # => ["\n", #<Process::Status: pid 2371774 exit 0>] Open3.capture2e('date > date.tmp') # Contains meta character. # => ["", #<Process::Status: pid 2371812 exit 0>]
The command line may also contain arguments and options for the command:
Open3.capture2e('echo "Foo"') # => ["Foo\n", #<Process::Status: pid 2326183 exit 0>]
Argument exe_path
Argument exe_path
is one of the following:
The string path to an executable to be called.
A 2-element array containing the path to an executable and the string to be used as the name of the executing process.
Example:
Open3.capture2e('/usr/bin/date') # => ["Sat Sep 30 09:01:46 AM CDT 2023\n", #<Process::Status: pid 2371820 exit 0>]
Ruby
invokes the executable directly, with no shell and no shell expansion:
Open3.capture2e('doesnt_exist') # Raises Errno::ENOENT
If one or more args
is given, each is an argument or option to be passed to the executable:
Open3.capture2e('echo', 'C #') # => ["C #\n", #<Process::Status: pid 2371856 exit 0>] Open3.capture2e('echo', 'hello', 'world') # => ["hello world\n", #<Process::Status: pid 2371894 exit 0>]
Create a new scope with the given locals and forwarding options that is suitable for passing into one of the Prism.* methods that accepts the ‘scopes` option.
Perform an operation in a block, raising an error if it takes longer than sec
seconds to complete.
sec
Number of seconds to wait for the block to terminate. Any non-negative number or nil may be used, including Floats to specify fractional seconds. A value of 0 or nil
will execute the block without any timeout. Any negative number will raise an ArgumentError
.
klass
Exception
Class
to raise if the block fails to terminate in sec
seconds. Omitting will use the default, Timeout::Error
message
Error
message to raise with Exception
Class
. Omitting will use the default, “execution expired”
Returns the result of the block if the block completed before sec
seconds, otherwise throws an exception, based on the value of klass
.
The exception thrown to terminate the given block cannot be rescued inside the block unless klass
is given explicitly. However, the block can use ensure to prevent the handling of the exception. For that reason, this method cannot be relied on to enforce timeouts for untrusted blocks.
If a scheduler is defined, it will be used to handle the timeout by invoking Scheduler#timeout_after.
Note that this is both a method of module Timeout
, so you can include Timeout
into your classes so they have a timeout
method, as well as a module method, so you can call it directly as Timeout.timeout()
.
Perform an operation in a block, raising an error if it takes longer than sec
seconds to complete.
sec
Number of seconds to wait for the block to terminate. Any non-negative number or nil may be used, including Floats to specify fractional seconds. A value of 0 or nil
will execute the block without any timeout. Any negative number will raise an ArgumentError
.
klass
Exception
Class
to raise if the block fails to terminate in sec
seconds. Omitting will use the default, Timeout::Error
message
Error
message to raise with Exception
Class
. Omitting will use the default, “execution expired”
Returns the result of the block if the block completed before sec
seconds, otherwise throws an exception, based on the value of klass
.
The exception thrown to terminate the given block cannot be rescued inside the block unless klass
is given explicitly. However, the block can use ensure to prevent the handling of the exception. For that reason, this method cannot be relied on to enforce timeouts for untrusted blocks.
If a scheduler is defined, it will be used to handle the timeout by invoking Scheduler#timeout_after.
Note that this is both a method of module Timeout
, so you can include Timeout
into your classes so they have a timeout
method, as well as a module method, so you can call it directly as Timeout.timeout()
.
Returns the value of the Gauss error function for x
.
Domain: [-INFINITY, INFINITY]
.
Range: [-1, 1]
.
Examples:
erf(-INFINITY) # => -1.0 erf(0.0) # => 0.0 erf(INFINITY) # => 1.0
Related: Math.erfc
.
Returns the value of the complementary error function for x
.
Domain: [-INFINITY, INFINITY]
.
Range: [0, 2]
.
Examples:
erfc(-INFINITY) # => 2.0 erfc(0.0) # => 1.0 erfc(INFINITY) # => 0.0
Related: Math.erf
.
Creates a new child process by doing one of the following in that process:
Passing string command_line
to the shell.
Invoking the executable at exe_path
.
This method has potential security vulnerabilities if called with untrusted input; see Command Injection.
Returns the process ID (pid) of the new process, without waiting for it to complete.
To avoid zombie processes, the parent process should call either:
Process.wait
, to collect the termination statuses of its children.
Process.detach
, to register disinterest in their status.
The new process is created using the exec system call; it may inherit some of its environment from the calling program (possibly including open file descriptors).
Argument env
, if given, is a hash that affects ENV
for the new process; see Execution Environment.
Argument options
is a hash of options for the new process; see Execution Options.
The first required argument is one of the following:
command_line
if it is a string, and if it begins with a shell reserved word or special built-in, or if it contains one or more meta characters.
exe_path
otherwise.
Argument command_line
String argument command_line
is a command line to be passed to a shell; it must begin with a shell reserved word, begin with a special built-in, or contain meta characters:
spawn('if true; then echo "Foo"; fi') # => 798847 # Shell reserved word. Process.wait # => 798847 spawn('exit') # => 798848 # Built-in. Process.wait # => 798848 spawn('date > /tmp/date.tmp') # => 798879 # Contains meta character. Process.wait # => 798849 spawn('date > /nop/date.tmp') # => 798882 # Issues error message. Process.wait # => 798882
The command line may also contain arguments and options for the command:
spawn('echo "Foo"') # => 799031 Process.wait # => 799031
Output:
Foo
See Execution Shell for details about the shell.
Raises an exception if the new process could not execute.
Argument exe_path
Argument exe_path
is one of the following:
The string path to an executable to be called.
A 2-element array containing the path to an executable to be called, and the string to be used as the name of the executing process.
spawn('/usr/bin/date') # Path to date on Unix-style system. Process.wait
Output:
Mon Aug 28 11:43:10 AM CDT 2023
Ruby
invokes the executable directly. This form does not use the shell; see Arguments args for caveats.
If one or more args
is given, each is an argument or option to be passed to the executable:
spawn('echo', 'C*') # => 799392 Process.wait # => 799392 spawn('echo', 'hello', 'world') # => 799393 Process.wait # => 799393
Output:
C* hello world
Raises an exception if the new process could not execute.
Sets the process group ID for the process given by process ID pid
to pgid
.
Not available on all platforms.
Establishes the current process as a new session and process group leader, with no controlling tty; returns the session ID:
Process.setsid # => 27422
Not available on all platforms.
Returns the scheduling priority for specified process, process group, or user.
Argument kind
is one of:
Process::PRIO_PROCESS
: return priority for process.
Process::PRIO_PGRP
: return priority for process group.
Process::PRIO_USER
: return priority for user.
Argument id
is the ID for the process, process group, or user; zero specified the current ID for kind
.
Examples:
Process.getpriority(Process::PRIO_USER, 0) # => 19 Process.getpriority(Process::PRIO_PROCESS, 0) # => 19
Not available on all platforms.
Notify the Ruby
virtual machine that the boot sequence is finished, and that now is a good time to optimize the application. This is useful for long running applications.
This method is expected to be called at the end of the application boot. If the application is deployed using a pre-forking model, Process.warmup
should be called in the original process before the first fork.
The actual optimizations performed are entirely implementation specific and may change in the future without notice.
On CRuby, Process.warmup
:
Performs a major GC
.
Compacts the heap.
Promotes all surviving objects to the old generation.
Precomputes the coderange of all strings.
Frees all empty heap pages and increments the allocatable pages counter by the number of pages freed.
Invoke malloc_trim
if available to free empty malloc pages.