Splits a string into an array of tokens in the same way the UNIX Bourne shell does.
argv = Shellwords.split('here are "two words"') argv #=> ["here", "are", "two words"]
Note, however, that this is not a command line parser. Shell metacharacters except for the single and double quotes and backslash are not treated as such.
argv = Shellwords.split('ruby my_prog.rb | less') argv #=> ["ruby", "my_prog.rb", "|", "less"]
String#shellsplit
is a shortcut for this function.
argv = 'here are "two words"'.shellsplit argv #=> ["here", "are", "two words"]
Splits a string into an array of tokens in the same way the UNIX Bourne shell does.
argv = Shellwords.split('here are "two words"') argv #=> ["here", "are", "two words"]
Note, however, that this is not a command line parser. Shell metacharacters except for the single and double quotes and backslash are not treated as such.
argv = Shellwords.split('ruby my_prog.rb | less') argv #=> ["ruby", "my_prog.rb", "|", "less"]
String#shellsplit
is a shortcut for this function.
argv = 'here are "two words"'.shellsplit argv #=> ["here", "are", "two words"]
Escapes a string so that it can be safely used in a Bourne shell command line. str
can be a non-string object that responds to to_s
.
Note that a resulted string should be used unquoted and is not intended for use in double quotes nor in single quotes.
argv = Shellwords.escape("It's better to give than to receive") argv #=> "It\\'s\\ better\\ to\\ give\\ than\\ to\\ receive"
String#shellescape
is a shorthand for this function.
argv = "It's better to give than to receive".shellescape argv #=> "It\\'s\\ better\\ to\\ give\\ than\\ to\\ receive" # Search files in lib for method definitions pattern = "^[ \t]*def " open("| grep -Ern -e #{pattern.shellescape} lib") { |grep| grep.each_line { |line| file, lineno, matched_line = line.split(':', 3) # ... } }
It is the caller’s responsibility to encode the string in the right encoding for the shell environment where this string is used.
Multibyte characters are treated as multibyte characters, not as bytes.
Returns an empty quoted String
if str
has a length of zero.
Escapes a string so that it can be safely used in a Bourne shell command line. str
can be a non-string object that responds to to_s
.
Note that a resulted string should be used unquoted and is not intended for use in double quotes nor in single quotes.
argv = Shellwords.escape("It's better to give than to receive") argv #=> "It\\'s\\ better\\ to\\ give\\ than\\ to\\ receive"
String#shellescape
is a shorthand for this function.
argv = "It's better to give than to receive".shellescape argv #=> "It\\'s\\ better\\ to\\ give\\ than\\ to\\ receive" # Search files in lib for method definitions pattern = "^[ \t]*def " open("| grep -Ern -e #{pattern.shellescape} lib") { |grep| grep.each_line { |line| file, lineno, matched_line = line.split(':', 3) # ... } }
It is the caller’s responsibility to encode the string in the right encoding for the shell environment where this string is used.
Multibyte characters are treated as multibyte characters, not as bytes.
Returns an empty quoted String
if str
has a length of zero.
Builds a command line string from an argument list, array
.
All elements are joined into a single string with fields separated by a space, where each element is escaped for the Bourne shell and stringified using to_s
.
ary = ["There's", "a", "time", "and", "place", "for", "everything"] argv = Shellwords.join(ary) argv #=> "There\\'s a time and place for everything"
Array#shelljoin
is a shortcut for this function.
ary = ["Don't", "rock", "the", "boat"] argv = ary.shelljoin argv #=> "Don\\'t rock the boat"
You can also mix non-string objects in the elements as allowed in Array#join
.
output = `#{['ps', '-p', $$].shelljoin}`
Builds a command line string from an argument list, array
.
All elements are joined into a single string with fields separated by a space, where each element is escaped for the Bourne shell and stringified using to_s
.
ary = ["There's", "a", "time", "and", "place", "for", "everything"] argv = Shellwords.join(ary) argv #=> "There\\'s a time and place for everything"
Array#shelljoin
is a shortcut for this function.
ary = ["Don't", "rock", "the", "boat"] argv = ary.shelljoin argv #=> "Don\\'t rock the boat"
You can also mix non-string objects in the elements as allowed in Array#join
.
output = `#{['ps', '-p', $$].shelljoin}`
Returns the singleton instance.
SyntaxSuggest.valid?
[Private]
Returns truthy if a given input source is valid syntax
SyntaxSuggest.valid?(<<~EOM) # => true def foo end EOM SyntaxSuggest.valid?(<<~EOM) # => false def foo def bar # Syntax error here end EOM
You can also pass in an array of lines and they’ll be joined before evaluating
SyntaxSuggest.valid?( [ "def foo\n", "end\n" ] ) # => true SyntaxSuggest.valid?( [ "def foo\n", " def bar\n", # Syntax error here "end\n" ] ) # => false
As an FYI the CodeLine
class instances respond to ‘to_s` so passing a CodeLine
in as an object or as an array will convert it to it’s code representation.
Returns the arc tangent of y
and x
in radians.
Domain of y
: [-INFINITY, INFINITY]
.
Domain of x
: [-INFINITY, INFINITY]
.
Range: [-PI, PI]
.
Examples:
atan2(-1.0, -1.0) # => -2.356194490192345 # -3*PI/4 atan2(-1.0, 0.0) # => -1.5707963267948966 # -PI/2 atan2(-1.0, 1.0) # => -0.7853981633974483 # -PI/4 atan2(0.0, -1.0) # => 3.141592653589793 # PI
Returns the tangent of x
in radians.
Domain: (-INFINITY, INFINITY)
.
Range: (-INFINITY, INFINITY)
.
Examples:
tan(-PI) # => 1.2246467991473532e-16 # -0.0000000000000001 tan(-PI/2) # => -1.633123935319537e+16 # -16331239353195370.0 tan(0.0) # => 0.0 tan(PI/2) # => 1.633123935319537e+16 # 16331239353195370.0 tan(PI) # => -1.2246467991473532e-16 # -0.0000000000000001
Returns the arc tangent of x
.
Domain: [-INFINITY, INFINITY]
.
Range: [-PI/2, PI/2]
.
Examples:
atan(-INFINITY) # => -1.5707963267948966 # -PI2 atan(-PI) # => -1.2626272556789115 atan(-PI/2) # => -1.0038848218538872 atan(0.0) # => 0.0 atan(PI/2) # => 1.0038848218538872 atan(PI) # => 1.2626272556789115 atan(INFINITY) # => 1.5707963267948966 # PI/2
Returns the hyperbolic tangent of x
in radians.
Domain: [-INFINITY, INFINITY]
.
Range: [-1, 1]
.
Examples:
tanh(-INFINITY) # => -1.0 tanh(0.0) # => 0.0 tanh(INFINITY) # => 1.0
Returns the inverse hyperbolic tangent of x
.
Domain: [-1, 1]
.
Range: [-INFINITY, INFINITY]
.
Examples:
atanh(-1.0) # => -Infinity atanh(0.0) # => 0.0 atanh(1.0) # => Infinity
Sends a signal to each process specified by ids
(which must specify at least one ID); returns the count of signals sent.
For each given id
, if id
is:
Positive, sends the signal to the process whose process ID is id
.
Zero, send the signal to all processes in the current process group.
Negative, sends the signal to a system-dependent collection of processes.
Argument signal
specifies the signal to be sent; the argument may be:
An integer signal number: e.g., -29
, 0
, 29
.
A signal name (string), with or without leading 'SIG'
, and with or without a further prefixed minus sign ('-'
): e.g.:
'SIGPOLL'
.
'POLL'
,
'-SIGPOLL'
.
'-POLL'
.
A signal symbol, with or without leading 'SIG'
, and with or without a further prefixed minus sign ('-'
): e.g.:
:SIGPOLL
.
:POLL
.
:'-SIGPOLL'
.
:'-POLL'
.
If signal
is:
A non-negative integer, or a signal name or symbol without prefixed '-'
, each process with process ID id
is signalled.
A negative integer, or a signal name or symbol with prefixed '-'
, each process group with group ID id
is signalled.
Use method Signal.list
to see which signals are supported by Ruby on the underlying platform; the method returns a hash of the string names and non-negative integer values of the supported signals. The size and content of the returned hash varies widely among platforms.
Additionally, signal 0
is useful to determine if the process exists.
Example:
pid = fork do Signal.trap('HUP') { puts 'Ouch!'; exit } # ... do some work ... end # ... Process.kill('HUP', pid) Process.wait
Output:
Ouch!
Exceptions:
Raises Errno::EINVAL or RangeError
if signal
is an integer but invalid.
Raises ArgumentError
if signal
is a string or symbol but invalid.
Raises Errno::ESRCH or RangeError
if one of ids
is invalid.
Raises Errno::EPERM if needed permissions are not in force.
In the last two cases, signals may have been sent to some processes.
Avoids the potential for a child process to become a zombie process. Process.detach
prevents this by setting up a separate Ruby thread whose sole job is to reap the status of the process pid when it terminates.
This method is needed only when the parent process will never wait for the child process.
This example does not reap the second child process; that process appears as a zombie in the process status (ps
) output:
pid = Process.spawn('ruby', '-e', 'exit 13') # => 312691 sleep(1) # Find zombies. system("ps -ho pid,state -p #{pid}")
Output:
312716 Z
This example also does not reap the second child process, but it does detach the process so that it does not become a zombie:
pid = Process.spawn('ruby', '-e', 'exit 13') # => 313213 thread = Process.detach(pid) sleep(1) # => #<Process::Waiter:0x00007f038f48b838 run> system("ps -ho pid,state -p #{pid}") # Finds no zombies.
The waiting thread can return the pid of the detached child process:
thread.join.pid # => 313262
Example:
x.foo ^^^^ x.foo(42) ^^^^ x&.foo ^^^^^ x[42] ^^^^ x += 1 ^