Terminates thr
and schedules another thread to be run, returning the terminated Thread
. If this is the main thread, or the last thread, exits the process.
Path of the file being run
If object is string-like, parse the string and return the parsed result as a Ruby data structure. Otherwise, generate a JSON
text from the Ruby data structure object and return it.
The opts argument is passed through to generate/parse respectively. See generate and parse for their documentation.
Creates a new Pathname
object from the given string, path
, and returns pathname object.
In order to use this constructor, you must first require the Pathname
standard library extension.
require 'pathname' Pathname("/home/zzak") #=> #<Pathname:/home/zzak>
See also Pathname::new
for more information.
Produces a shallow copy of obj—the instance variables of obj are copied, but not the objects they reference. clone
copies the frozen value state of obj, unless the :freeze
keyword argument is given with a false or true value. See also the discussion under Object#dup
.
class Klass attr_accessor :str end s1 = Klass.new #=> #<Klass:0x401b3a38> s1.str = "Hello" #=> "Hello" s2 = s1.clone #=> #<Klass:0x401b3998 @str="Hello"> s2.str[1,4] = "i" #=> "i" s1.inspect #=> "#<Klass:0x401b3a38 @str=\"Hi\">" s2.inspect #=> "#<Klass:0x401b3998 @str=\"Hi\">"
This method may have class-specific behavior. If so, that behavior will be documented under the #initialize_copy
method of the class.
Returns an array converted from object
.
Tries to convert object
to an array using to_ary
first and to_a
second:
Array([0, 1, 2]) # => [0, 1, 2] Array({foo: 0, bar: 1}) # => [[:foo, 0], [:bar, 1]] Array(0..4) # => [0, 1, 2, 3, 4]
Returns object
in an array, [object]
, if object
cannot be converted:
Array(:foo) # => [:foo]
Exits the process immediately; no exit handlers are called. Returns exit status status
to the underlying operating system.
Process.exit!(true)
Values true
and false
for argument status
indicate, respectively, success and failure; The meanings of integer values are system-dependent.
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('echo') # => 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:
spawn('/usr/bin/date') # Path to date on Unix-style system. Process.wait
Output:
Thu Aug 31 10:06:48 AM CDT 2023
A 2-element array containing the path to an executable and the string to be used as the name of the executing process:
pid = spawn(['sleep', 'Hello!'], '1') # 2-element array. p `ps -p #{pid} -o command=`
Output:
"Hello! 1\n"
Ruby invokes the executable directly, with no shell and no shell expansion.
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.
Initiates termination of the Ruby script by raising SystemExit
; the exception may be caught. Returns exit status status
to the underlying operating system.
Values true
and false
for argument status
indicate, respectively, success and failure; The meanings of integer values are system-dependent.
Example:
begin exit puts 'Never get here.' rescue SystemExit puts 'Rescued a SystemExit exception.' end puts 'After begin block.'
Output:
Rescued a SystemExit exception. After begin block.
Just prior to final termination, Ruby executes any at-exit procedures (see Kernel::at_exit) and any object finalizers (see ObjectSpace::define_finalizer
).
Example:
at_exit { puts 'In at_exit function.' } ObjectSpace.define_finalizer('string', proc { puts 'In finalizer.' }) exit
Output:
In at_exit function. In finalizer.
Terminates execution immediately, effectively by calling Kernel.exit(false)
.
If string argument msg
is given, it is written to STDERR prior to termination; otherwise, if an exception was raised, prints its message and backtrace.
Deprecated. Use block_given? instead.
If warnings have been disabled (for example with the -W0
flag), does nothing. Otherwise, converts each of the messages to strings, appends a newline character to the string if the string does not end in a newline, and calls Warning.warn
with the string.
warn("warning 1", "warning 2")
produces:
warning 1 warning 2
If the uplevel
keyword argument is given, the string will be prepended with information for the given caller frame in the same format used by the rb_warn
C function.
# In baz.rb def foo warn("invalid call to foo", uplevel: 1) end def bar foo end bar
produces:
baz.rb:6: warning: invalid call to foo
If category
keyword argument is given, passes the category to Warning.warn
. The category given must be be one of the following categories:
Used for warning for deprecated functionality that may be removed in the future.
Used for experimental features that may change in future releases.
Returns an array containing the sorted elements of self
. The ordering of equal elements is indeterminate and may be unstable.
With no block given, the sort compares using the elements’ own method <=>
:
%w[b c a d].sort # => ["a", "b", "c", "d"] {foo: 0, bar: 1, baz: 2}.sort # => [[:bar, 1], [:baz, 2], [:foo, 0]]
With a block given, comparisons in the block determine the ordering. The block is called with two elements a
and b
, and must return:
A negative integer if a < b
.
Zero if a == b
.
A positive integer if a > b
.
Examples:
a = %w[b c a d] a.sort {|a, b| b <=> a } # => ["d", "c", "b", "a"] h = {foo: 0, bar: 1, baz: 2} h.sort {|a, b| b <=> a } # => [[:foo, 0], [:baz, 2], [:bar, 1]]
See also sort_by
. It implements a Schwartzian transform which is useful when key computation or comparison is expensive.
Returns whether exactly one element meets a given criterion.
With no argument and no block, returns whether exactly one element is truthy:
(1..1).one? # => true [1, nil, false].one? # => true (1..4).one? # => false {foo: 0}.one? # => true {foo: 0, bar: 1}.one? # => false [].one? # => false
With argument pattern
and no block, returns whether for exactly one element element
, pattern === element
:
[nil, false, 0].one?(Integer) # => true [nil, false, 0].one?(Numeric) # => true [nil, false, 0].one?(Float) # => false %w[bar baz bat bam].one?(/m/) # => true %w[bar baz bat bam].one?(/foo/) # => false %w[bar baz bat bam].one?('ba') # => false {foo: 0, bar: 1, baz: 2}.one?(Array) # => false {foo: 0}.one?(Array) # => true [].one?(Integer) # => false
With a block given, returns whether the block returns a truthy value for exactly one element:
(1..4).one? {|element| element < 2 } # => true (1..4).one? {|element| element < 1 } # => false {foo: 0, bar: 1, baz: 2}.one? {|key, value| value < 1 } # => true {foo: 0, bar: 1, baz: 2}.one? {|key, value| value < 2 } # => false
Returns whether no element meets a given criterion.
With no argument and no block, returns whether no element is truthy:
(1..4).none? # => false [nil, false].none? # => true {foo: 0}.none? # => false {foo: 0, bar: 1}.none? # => false [].none? # => true
With argument pattern
and no block, returns whether for no element element
, pattern === element
:
[nil, false, 1.1].none?(Integer) # => true %w[bar baz bat bam].none?(/m/) # => false %w[bar baz bat bam].none?(/foo/) # => true %w[bar baz bat bam].none?('ba') # => true {foo: 0, bar: 1, baz: 2}.none?(Hash) # => true {foo: 0}.none?(Array) # => false [].none?(Integer) # => true
With a block given, returns whether the block returns a truthy value for no element:
(1..4).none? {|element| element < 1 } # => true (1..4).none? {|element| element < 2 } # => false {foo: 0, bar: 1, baz: 2}.none? {|key, value| value < 0 } # => true {foo: 0, bar: 1, baz: 2}.none? {|key, value| value < 1 } # => false
Returns an array of all non-nil
elements:
a = [nil, 0, nil, 'a', false, nil, false, nil, 'a', nil, 0, nil] a.compact # => [0, "a", false, false, "a", 0]
Writes warning message msg
to $stderr. This method is called by Ruby for all emitted warnings. A category
may be included with the warning.
See the documentation of the Warning
module for how to customize this.
Computes the square root of decimal
to the specified number of digits of precision, numeric
.
BigMath.sqrt(BigDecimal('2'), 16).to_s #=> "0.1414213562373095048801688724e1"
Returns true if coverage measurement is supported for the given mode.
The mode should be one of the following symbols: :lines
, :oneshot_lines
, :branches
, :methods
, :eval
.
Example:
Coverage.supported?(:lines) #=> true Coverage.supported?(:all) #=> false
Provides a convenient Ruby iterator which executes a block for each entry in the /etc/passwd
file.
The code block is passed an Passwd
struct.
See ::getpwent
above for details.
Example:
require 'etc' Etc.passwd {|u| puts u.name + " = " + u.gecos }
Returns system configuration directory.
This is typically "/etc"
, but is modified by the prefix used when Ruby was compiled. For example, if Ruby is built and installed in /usr/local
, returns "/usr/local/etc"
on other platforms than Windows.
On Windows, this always returns the directory provided by the system.
Returns system configuration variable using sysconf().
name should be a constant under Etc
which begins with SC_
.
The return value is an integer or nil. nil means indefinite limit. (sysconf() returns -1 but errno is not set.)
Etc.sysconf(Etc::SC_ARG_MAX) #=> 2097152 Etc.sysconf(Etc::SC_LOGIN_NAME_MAX) #=> 256
Returns system configuration variable using confstr().
name should be a constant under Etc
which begins with CS_
.
The return value is a string or nil. nil means no configuration-defined value. (confstr() returns 0 but errno is not set.)
Etc.confstr(Etc::CS_PATH) #=> "/bin:/usr/bin" # GNU/Linux Etc.confstr(Etc::CS_GNU_LIBC_VERSION) #=> "glibc 2.18" Etc.confstr(Etc::CS_GNU_LIBPTHREAD_VERSION) #=> "NPTL 2.18"
Encodes string using String.encode
.