Returns an indication of the number of arguments accepted by a method. Returns a nonnegative integer for methods that take a fixed number of arguments. For Ruby methods that take a variable number of arguments, returns -n-1, where n is the number of required arguments. Keyword arguments will be considered as a single additional argument, that argument being mandatory if any keyword argument is mandatory. For methods written in C, returns -1 if the call takes a variable number of arguments.
class C def one; end def two(a); end def three(*a); end def four(a, b); end def five(a, b, *c); end def six(a, b, *c, &d); end def seven(a, b, x:0); end def eight(x:, y:); end def nine(x:, y:, **z); end def ten(*a, x:, y:); end end c = C.new c.method(:one).arity #=> 0 c.method(:two).arity #=> 1 c.method(:three).arity #=> -1 c.method(:four).arity #=> 2 c.method(:five).arity #=> -3 c.method(:six).arity #=> -3 c.method(:seven).arity #=> -3 c.method(:eight).arity #=> 1 c.method(:nine).arity #=> 1 c.method(:ten).arity #=> -2 "cat".method(:size).arity #=> 0 "cat".method(:replace).arity #=> 1 "cat".method(:squeeze).arity #=> -1 "cat".method(:count).arity #=> -1
Returns an indication of the number of arguments accepted by a method. Returns a nonnegative integer for methods that take a fixed number of arguments. For Ruby methods that take a variable number of arguments, returns -n-1, where n is the number of required arguments. Keyword arguments will be considered as a single additional argument, that argument being mandatory if any keyword argument is mandatory. For methods written in C, returns -1 if the call takes a variable number of arguments.
class C def one; end def two(a); end def three(*a); end def four(a, b); end def five(a, b, *c); end def six(a, b, *c, &d); end def seven(a, b, x:0); end def eight(x:, y:); end def nine(x:, y:, **z); end def ten(*a, x:, y:); end end c = C.new c.method(:one).arity #=> 0 c.method(:two).arity #=> 1 c.method(:three).arity #=> -1 c.method(:four).arity #=> 2 c.method(:five).arity #=> -3 c.method(:six).arity #=> -3 c.method(:seven).arity #=> -3 c.method(:eight).arity #=> 1 c.method(:nine).arity #=> 1 c.method(:ten).arity #=> -2 "cat".method(:size).arity #=> 0 "cat".method(:replace).arity #=> 1 "cat".method(:squeeze).arity #=> -1 "cat".method(:count).arity #=> -1
Returns a string, using platform providing features. Returned value is expected to be a cryptographically secure pseudo-random number in binary form. This method raises a RuntimeError
if the feature provided by platform failed to prepare the result.
In 2017, Linux manpage random(7) writes that “no cryptographic primitive available today can hope to promise more than 256 bits of security”. So it might be questionable to pass size > 32 to this method.
Random.urandom(8) #=> "\x78\x41\xBA\xAF\x7D\xEA\xD8\xEA"
Equivalent to:
io.write(sprintf(format_string, *objects))
For details on format_string
, see Format Specifications.
With the single argument format_string
, formats objects
into the string, then writes the formatted string to $stdout:
printf('%4.4d %10s %2.2f', 24, 24, 24.0)
Output (on $stdout):
0024 24 24.00#
With arguments io
and format_string
, formats objects
into the string, then writes the formatted string to io
:
printf($stderr, '%4.4d %10s %2.2f', 24, 24, 24.0)
Output (on $stderr):
0024 24 24.00# => nil
With no arguments, does nothing.
Equivalent to $stdout.print(*objects)
, this method is the straightforward way to write to $stdout
.
Writes the given objects to $stdout
; returns nil
. Appends the output record separator $OUTPUT_RECORD_SEPARATOR
$\
), if it is not nil
.
With argument objects
given, for each object:
Converts via its method to_s
if not a string.
Writes to stdout
.
If not the last object, writes the output field separator $OUTPUT_FIELD_SEPARATOR
($,
if it is not nil
.
With default separators:
objects = [0, 0.0, Rational(0, 1), Complex(0, 0), :zero, 'zero'] $OUTPUT_RECORD_SEPARATOR $OUTPUT_FIELD_SEPARATOR print(*objects)
Output:
nil nil 00.00/10+0izerozero
With specified separators:
$OUTPUT_RECORD_SEPARATOR = "\n" $OUTPUT_FIELD_SEPARATOR = ',' print(*objects)
Output:
0,0.0,0/1,0+0i,zero,zero
With no argument given, writes the content of $_
(which is usually the most recent user input):
gets # Sets $_ to the most recent user input. print # Prints $_.
Returns the string resulting from formatting objects
into format_string
.
For details on format_string
, see Format Specifications.
Returns a string converted from object
.
Tries to convert object
to a string using to_str
first and to_s
second:
String([0, 1, 2]) # => "[0, 1, 2]" String(0..5) # => "0..5" String({foo: 0, bar: 1}) # => "{:foo=>0, :bar=>1}"
Raises TypeError
if object
cannot be converted to a string.
Returns true
if the named file is writable by the effective user and group id of this process. See eaccess(3).
Note that some OS-level security features may cause this to return true even though the file is not writable by the effective user/group.
Returns the time used to execute the given block as a Benchmark::Tms
object. Takes label
option.
require 'benchmark' n = 1000000 time = Benchmark.measure do n.times { a = "1" } end puts time
Generates:
0.220000 0.000000 0.220000 ( 0.227313)
Returns the time used to execute the given block as a Benchmark::Tms
object. Takes label
option.
require 'benchmark' n = 1000000 time = Benchmark.measure do n.times { a = "1" } end puts time
Generates:
0.220000 0.000000 0.220000 ( 0.227313)
The standard configuration object for gems.
Use the given configuration object (which implements the ConfigFile
protocol) as the standard configuration object.
Returns an Array
of sources to fetch remote gems from. Uses default_sources
if the sources list is empty.
Need to be able to set the sources without calling Gem.sources
.replace since that would cause an infinite loop.
DOC: This comment is not documentation about the method itself, it’s more of a code comment about the implementation.
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>]
Write bytes in str
to the location pointed to by address
.
Performs a Miller-Rabin probabilistic primality test for bn
.
checks
parameter is deprecated in version 3.0. It has no effect.