Set
array of platforms this RubyGems supports (primarily for testing).
Array
of platforms this RubyGems supports.
Prints the amount of time the supplied block takes to run using the debug UI output.
Returns the arc tangent of z
CMath.atan(1 + 1i) #=> (1.0172219678978514+0.4023594781085251i)
returns the arc tangent of y
divided by x
using the signs of y
and x
to determine the quadrant
CMath.atan2(1 + 1i, 0) #=> (1.5707963267948966+0.0i)
returns the inverse hyperbolic tangent of z
CMath.atanh(1 + 1i) #=> (0.4023594781085251+1.0172219678978514i)
Returns the arc tangent of z
CMath.atan(1 + 1i) #=> (1.0172219678978514+0.4023594781085251i)
returns the arc tangent of y
divided by x
using the signs of y
and x
to determine the quadrant
CMath.atan2(1 + 1i, 0) #=> (1.5707963267948966+0.0i)
returns the inverse hyperbolic tangent of z
CMath.atanh(1 + 1i) #=> (0.4023594781085251+1.0172219678978514i)
Get the thread of the primary server.
This returns nil if there is no primary server. See primary_server
.
Get the thread of the primary server.
This returns nil if there is no primary server. See primary_server
.
Returns true if new
is newer than all old_list
. Non-existent files are older than any file.
FileUtils.uptodate?('hello.o', %w(hello.c hello.h)) or \ system 'make hello.o'
Returns true if new
is newer than all old_list
. Non-existent files are older than any file.
FileUtils.uptodate?('hello.o', %w(hello.c hello.h)) or \ system 'make hello.o'
URI::parse(uri_str)
Creates one of the URI’s subclasses instance from the string.
URI::InvalidURIError
Raised if URI
given is not a correct one.
require 'uri' uri = URI.parse("http://www.ruby-lang.org/") # => #<URI::HTTP http://www.ruby-lang.org/> uri.scheme # => "http" uri.host # => "www.ruby-lang.org"
It’s recommended to first ::escape the provided uri_str
if there are any invalid URI
characters.
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 number may be used, including Floats to specify fractional seconds. A value of 0 or nil
will execute the block without any timeout.
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.
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 number may be used, including Floats to specify fractional seconds. A value of 0 or nil
will execute the block without any timeout.
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.
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()
.
Computes the arc tangent given y
and x
. Returns a Float
in the range -PI..PI. Return value is a angle in radians between the positive x-axis of cartesian plane and the point given by the coordinates (x
, y
) on it.
Domain: (-INFINITY, INFINITY)
Codomain: [-PI, PI]
Math.atan2(-0.0, -1.0) #=> -3.141592653589793 Math.atan2(-1.0, -1.0) #=> -2.356194490192345 Math.atan2(-1.0, 0.0) #=> -1.5707963267948966 Math.atan2(-1.0, 1.0) #=> -0.7853981633974483 Math.atan2(-0.0, 1.0) #=> -0.0 Math.atan2(0.0, 1.0) #=> 0.0 Math.atan2(1.0, 1.0) #=> 0.7853981633974483 Math.atan2(1.0, 0.0) #=> 1.5707963267948966 Math.atan2(1.0, -1.0) #=> 2.356194490192345 Math.atan2(0.0, -1.0) #=> 3.141592653589793 Math.atan2(INFINITY, INFINITY) #=> 0.7853981633974483 Math.atan2(INFINITY, -INFINITY) #=> 2.356194490192345 Math.atan2(-INFINITY, INFINITY) #=> -0.7853981633974483 Math.atan2(-INFINITY, -INFINITY) #=> -2.356194490192345
Computes the arc tangent of x
. Returns -PI/2..PI/2.
Domain: (-INFINITY, INFINITY)
Codomain: (-PI/2, PI/2)
Math.atan(0) #=> 0.0
Computes the inverse hyperbolic tangent of x
.
Domain: (-1, 1)
Codomain: (-INFINITY, INFINITY)
Math.atanh(1) #=> Infinity
Calculates the gamma function of x.
Note that gamma(n) is same as fact(n-1) for integer n > 0. However gamma(n) returns float and can be an approximation.
def fact(n) (1..n).inject(1) {|r,i| r*i } end 1.upto(26) {|i| p [i, Math.gamma(i), fact(i-1)] } #=> [1, 1.0, 1] # [2, 1.0, 1] # [3, 2.0, 2] # [4, 6.0, 6] # [5, 24.0, 24] # [6, 120.0, 120] # [7, 720.0, 720] # [8, 5040.0, 5040] # [9, 40320.0, 40320] # [10, 362880.0, 362880] # [11, 3628800.0, 3628800] # [12, 39916800.0, 39916800] # [13, 479001600.0, 479001600] # [14, 6227020800.0, 6227020800] # [15, 87178291200.0, 87178291200] # [16, 1307674368000.0, 1307674368000] # [17, 20922789888000.0, 20922789888000] # [18, 355687428096000.0, 355687428096000] # [19, 6.402373705728e+15, 6402373705728000] # [20, 1.21645100408832e+17, 121645100408832000] # [21, 2.43290200817664e+18, 2432902008176640000] # [22, 5.109094217170944e+19, 51090942171709440000] # [23, 1.1240007277776077e+21, 1124000727777607680000] # [24, 2.5852016738885062e+22, 25852016738884976640000] # [25, 6.204484017332391e+23, 620448401733239439360000] # [26, 1.5511210043330954e+25, 15511210043330985984000000]
Calculates the logarithmic gamma of x
and the sign of gamma of x
.
Math.lgamma(x)
is same as
[Math.log(Math.gamma(x).abs), Math.gamma(x) < 0 ? -1 : 1]
but avoid overflow by Math.gamma(x)
for large x.
Math.lgamma(0) #=> [Infinity, 1]
spawn executes specified command and return its pid.
pid = spawn("tar xf ruby-2.0.0-p195.tar.bz2") Process.wait pid pid = spawn(RbConfig.ruby, "-eputs'Hello, world!'") Process.wait pid
This method is similar to Kernel#system
but it doesn’t wait for the command to finish.
The parent process should use Process.wait
to collect the termination status of its child or use Process.detach
to register disinterest in their status; otherwise, the operating system may accumulate zombie processes.
spawn has bunch of options to specify process attributes:
env: hash name => val : set the environment variable name => nil : unset the environment variable the keys and the values except for +nil+ must be strings. command...: commandline : command line string which is passed to the standard shell cmdname, arg1, ... : command name and one or more arguments (This form does not use the shell. See below for caveats.) [cmdname, argv0], arg1, ... : command name, argv[0] and zero or more arguments (no shell) options: hash clearing environment variables: :unsetenv_others => true : clear environment variables except specified by env :unsetenv_others => false : don't clear (default) process group: :pgroup => true or 0 : make a new process group :pgroup => pgid : join the specified process group :pgroup => nil : don't change the process group (default) create new process group: Windows only :new_pgroup => true : the new process is the root process of a new process group :new_pgroup => false : don't create a new process group (default) resource limit: resourcename is core, cpu, data, etc. See Process.setrlimit. :rlimit_resourcename => limit :rlimit_resourcename => [cur_limit, max_limit] umask: :umask => int redirection: key: FD : single file descriptor in child process [FD, FD, ...] : multiple file descriptor in child process value: FD : redirect to the file descriptor in parent process string : redirect to file with open(string, "r" or "w") [string] : redirect to file with open(string, File::RDONLY) [string, open_mode] : redirect to file with open(string, open_mode, 0644) [string, open_mode, perm] : redirect to file with open(string, open_mode, perm) [:child, FD] : redirect to the redirected file descriptor :close : close the file descriptor in child process FD is one of follows :in : the file descriptor 0 which is the standard input :out : the file descriptor 1 which is the standard output :err : the file descriptor 2 which is the standard error integer : the file descriptor of specified the integer io : the file descriptor specified as io.fileno file descriptor inheritance: close non-redirected non-standard fds (3, 4, 5, ...) or not :close_others => false : inherit current directory: :chdir => str The 'cmdname, arg1, ...' form does not use the shell. However, on different OSes, different things are provided as built-in commands. An example of this is 'echo', which is a built-in on Windows, but is a normal program on Linux and Mac OS X. This means that `Process.spawn 'echo', '%Path%'` will display the contents of the `%Path%` environment variable on Windows, but `Process.spawn 'echo', '$PATH'` prints the literal '$PATH'.
If a hash is given as env
, the environment is updated by env
before exec(2)
in the child process. If a pair in env
has nil as the value, the variable is deleted.
# set FOO as BAR and unset BAZ. pid = spawn({"FOO"=>"BAR", "BAZ"=>nil}, command)
If a hash is given as options
, it specifies process group, create new process group, resource limit, current directory, umask and redirects for the child process. Also, it can be specified to clear environment variables.
The :unsetenv_others
key in options
specifies to clear environment variables, other than specified by env
.
pid = spawn(command, :unsetenv_others=>true) # no environment variable pid = spawn({"FOO"=>"BAR"}, command, :unsetenv_others=>true) # FOO only
The :pgroup
key in options
specifies a process group. The corresponding value should be true, zero, a positive integer, or nil. true and zero cause the process to be a process leader of a new process group. A non-zero positive integer causes the process to join the provided process group. The default value, nil, causes the process to remain in the same process group.
pid = spawn(command, :pgroup=>true) # process leader pid = spawn(command, :pgroup=>10) # belongs to the process group 10
The :new_pgroup
key in options
specifies to pass CREATE_NEW_PROCESS_GROUP
flag to CreateProcessW()
that is Windows API. This option is only for Windows. true means the new process is the root process of the new process group. The new process has CTRL+C disabled. This flag is necessary for Process.kill(:SIGINT, pid)
on the subprocess. :new_pgroup is false by default.
pid = spawn(command, :new_pgroup=>true) # new process group pid = spawn(command, :new_pgroup=>false) # same process group
The :rlimit_
foo key specifies a resource limit. foo should be one of resource types such as core
. The corresponding value should be an integer or an array which have one or two integers: same as cur_limit and max_limit arguments for Process.setrlimit
.
cur, max = Process.getrlimit(:CORE) pid = spawn(command, :rlimit_core=>[0,max]) # disable core temporary. pid = spawn(command, :rlimit_core=>max) # enable core dump pid = spawn(command, :rlimit_core=>0) # never dump core.
The :umask
key in options
specifies the umask.
pid = spawn(command, :umask=>077)
The :in, :out, :err, an integer, an IO
and an array key specifies a redirection. The redirection maps a file descriptor in the child process.
For example, stderr can be merged into stdout as follows:
pid = spawn(command, :err=>:out) pid = spawn(command, 2=>1) pid = spawn(command, STDERR=>:out) pid = spawn(command, STDERR=>STDOUT)
The hash keys specifies a file descriptor in the child process started by spawn
. :err, 2 and STDERR specifies the standard error stream (stderr).
The hash values specifies a file descriptor in the parent process which invokes spawn
. :out, 1 and STDOUT specifies the standard output stream (stdout).
In the above example, the standard output in the child process is not specified. So it is inherited from the parent process.
The standard input stream (stdin) can be specified by :in, 0 and STDIN.
A filename can be specified as a hash value.
pid = spawn(command, :in=>"/dev/null") # read mode pid = spawn(command, :out=>"/dev/null") # write mode pid = spawn(command, :err=>"log") # write mode pid = spawn(command, [:out, :err]=>"/dev/null") # write mode pid = spawn(command, 3=>"/dev/null") # read mode
For stdout and stderr (and combination of them), it is opened in write mode. Otherwise read mode is used.
For specifying flags and permission of file creation explicitly, an array is used instead.
pid = spawn(command, :in=>["file"]) # read mode is assumed pid = spawn(command, :in=>["file", "r"]) pid = spawn(command, :out=>["log", "w"]) # 0644 assumed pid = spawn(command, :out=>["log", "w", 0600]) pid = spawn(command, :out=>["log", File::WRONLY|File::EXCL|File::CREAT, 0600])
The array specifies a filename, flags and permission. The flags can be a string or an integer. If the flags is omitted or nil, File::RDONLY is assumed. The permission should be an integer. If the permission is omitted or nil, 0644 is assumed.
If an array of IOs and integers are specified as a hash key, all the elements are redirected.
# stdout and stderr is redirected to log file. # The file "log" is opened just once. pid = spawn(command, [:out, :err]=>["log", "w"])
Another way to merge multiple file descriptors is [:child, fd]. [:child, fd] means the file descriptor in the child process. This is different from fd. For example, :err=>:out means redirecting child stderr to parent stdout. But :err=>[:child, :out] means redirecting child stderr to child stdout. They differ if stdout is redirected in the child process as follows.
# stdout and stderr is redirected to log file. # The file "log" is opened just once. pid = spawn(command, :out=>["log", "w"], :err=>[:child, :out])
[:child, :out] can be used to merge stderr into stdout in IO.popen
. In this case, IO.popen
redirects stdout to a pipe in the child process and [:child, :out] refers the redirected stdout.
io = IO.popen(["sh", "-c", "echo out; echo err >&2", :err=>[:child, :out]]) p io.read #=> "out\nerr\n"
The :chdir
key in options
specifies the current directory.
pid = spawn(command, :chdir=>"/var/tmp")
spawn closes all non-standard unspecified descriptors by default. The “standard” descriptors are 0, 1 and 2. This behavior is specified by :close_others option. :close_others doesn’t affect the standard descriptors which are closed only if :close is specified explicitly.
pid = spawn(command, :close_others=>true) # close 3,4,5,... (default) pid = spawn(command, :close_others=>false) # don't close 3,4,5,...
:close_others is false by default for spawn and IO.popen
.
Note that fds which close-on-exec flag is already set are closed regardless of :close_others option.
So IO.pipe
and spawn can be used as IO.popen
.
# similar to r = IO.popen(command) r, w = IO.pipe pid = spawn(command, :out=>w) # r, w is closed in the child process. w.close
:close is specified as a hash value to close a fd individually.
f = open(foo) system(command, f=>:close) # don't inherit f.
If a file descriptor need to be inherited, io=>io can be used.
# valgrind has --log-fd option for log destination. # log_w=>log_w indicates log_w.fileno inherits to child process. log_r, log_w = IO.pipe pid = spawn("valgrind", "--log-fd=#{log_w.fileno}", "echo", "a", log_w=>log_w) log_w.close p log_r.read
It is also possible to exchange file descriptors.
pid = spawn(command, :out=>:err, :err=>:out)
The hash keys specify file descriptors in the child process. The hash values specifies file descriptors in the parent process. So the above specifies exchanging stdout and stderr. Internally, spawn
uses an extra file descriptor to resolve such cyclic file descriptor mapping.
See Kernel.exec
for the standard shell.
Returns a Tms
structure (see Process::Tms
) that contains user and system CPU times for this process, and also for children processes.
t = Process.times [ t.utime, t.stime, t.cutime, t.cstime ] #=> [0.0, 0.02, 0.00, 0.00]