Compile a ClassVariableOperatorWriteNode
node
Compile a ConstantPathOperatorWriteNode
node
Compile a GlobalVariableOperatorWriteNode
node
Compile a InstanceVariableOperatorWriteNode
node
@@foo += bar
becomes
@@foo = @@foo + bar
$foo += bar
becomes
$foo = $foo + bar
@foo += bar
becomes
@foo = @foo + bar
Dispatch enter and leave events for ClassVariableOperatorWriteNode
nodes and continue walking the tree.
Dispatch enter and leave events for ConstantPathOperatorWriteNode
nodes and continue walking the tree.
Dispatch enter and leave events for GlobalVariableOperatorWriteNode
nodes and continue walking the tree.
Dispatch enter and leave events for InstanceVariableOperatorWriteNode
nodes and continue walking the tree.
Copy a ClassVariableOperatorWriteNode
node
Copy a ConstantPathOperatorWriteNode
node
Copy a GlobalVariableOperatorWriteNode
node
Copy a InstanceVariableOperatorWriteNode
node
Replaces the current process by doing one of the following:
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.
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:
exec('if true; then echo "Foo"; fi') # Shell reserved word. exec('echo') # Built-in. exec('date > date.tmp') # Contains meta character.
The command line may also contain arguments and options for the command:
exec('echo "Foo"')
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 and the string to be used as the name of the executing process.
Example:
exec('/usr/bin/date')
Output:
Sat Aug 26 09:38:00 AM CDT 2023
Ruby invokes the executable directly, with no shell and no shell expansion:
exec('doesnt_exist') # Raises Errno::ENOENT
If one or more args
is given, each is an argument or option to be passed to the executable:
exec('echo', 'C*') exec('echo', 'hello', 'world')
Output:
C* hello world
Raises an exception if the new process could not execute.
Creates a child process.
With a block given, runs the block in the child process; on block exit, the child terminates with a status of zero:
puts "Before the fork: #{Process.pid}" fork do puts "In the child process: #{Process.pid}" end # => 382141 puts "After the fork: #{Process.pid}"
Output:
Before the fork: 420496 After the fork: 420496 In the child process: 420520
With no block given, the fork
call returns twice:
Once in the parent process, returning the pid of the child process.
Once in the child process, returning nil
.
Example:
puts "This is the first line before the fork (pid #{Process.pid})" puts fork puts "This is the second line after the fork (pid #{Process.pid})"
Output:
This is the first line before the fork (pid 420199) 420223 This is the second line after the fork (pid 420199) This is the second line after the fork (pid 420223)
In either case, the child process may exit using Kernel.exit!
to avoid the call to Kernel#at_exit
.
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 thread calling fork
is the only thread in the created child process; fork
doesn’t copy other threads.
Note that method fork
is available on some platforms, but not on others:
Process.respond_to?(:fork) # => true # Would be false on some.
If not, you may use ::spawn
instead of fork
.
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.
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.
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.
Returns a Process::Status
object representing the most recently exited child process in the current thread, or nil
if none:
Process.spawn('ruby', '-e', 'exit 13') Process.wait Process.last_status # => #<Process::Status: pid 14396 exit 13> Process.spawn('ruby', '-e', 'exit 14') Process.wait Process.last_status # => #<Process::Status: pid 4692 exit 14> Process.spawn('ruby', '-e', 'exit 15') # 'exit 15' has not been reaped by #wait. Process.last_status # => #<Process::Status: pid 4692 exit 14> Process.wait Process.last_status # => #<Process::Status: pid 1380 exit 15>