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 the parameter information of this method.
def foo(bar); end method(:foo).parameters #=> [[:req, :bar]] def foo(bar, baz, bat, &blk); end method(:foo).parameters #=> [[:req, :bar], [:req, :baz], [:req, :bat], [:block, :blk]] def foo(bar, *args); end method(:foo).parameters #=> [[:req, :bar], [:rest, :args]] def foo(bar, baz, *args, &blk); end method(:foo).parameters #=> [[:req, :bar], [:req, :baz], [:rest, :args], [:block, :blk]]
Basically the same as ::new
. However, if class Thread
is subclassed, then calling start
in that subclass will not invoke the subclass’s initialize
method.
Returns the priority of thr. Default is inherited from the current thread which creating the new thread, or zero for the initial main thread; higher-priority thread will run more frequently than lower-priority threads (but lower-priority threads can also run).
This is just hint for Ruby
thread scheduler. It may be ignored on some platform.
Thread.current.priority #=> 0
Sets the priority of thr to integer. Higher-priority threads will run more frequently than lower-priority threads (but lower-priority threads can also run).
This is just hint for Ruby
thread scheduler. It may be ignored on some platform.
count1 = count2 = 0 a = Thread.new do loop { count1 += 1 } end a.priority = -1 b = Thread.new do loop { count2 += 1 } end b.priority = -2 sleep 1 #=> 1 count1 #=> 622504 count2 #=> 5832
Returns the parameter definitions of the method or block that the current hook belongs to. The format is the same as for Method#parameters
.
Enables the coverage measurement. See the documentation of Coverage
class in detail. This is equivalent to Coverage.setup
and Coverage.resume
.
Returns the Ruby
objects created by parsing the given source
.
Argument source
contains the String to be parsed.
Argument opts
, if given, contains a Hash of options for the parsing. See Parsing Options.
When source
is a JSON array, returns a Ruby
Array:
source = '["foo", 1.0, true, false, null]' ruby = JSON.parse(source) ruby # => ["foo", 1.0, true, false, nil] ruby.class # => Array
When source
is a JSON object, returns a Ruby
Hash:
source = '{"a": "foo", "b": 1.0, "c": true, "d": false, "e": null}' ruby = JSON.parse(source) ruby # => {"a"=>"foo", "b"=>1.0, "c"=>true, "d"=>false, "e"=>nil} ruby.class # => Hash
For examples of parsing for all JSON data types, see Parsing JSON.
Parses nested JSON
objects:
source = <<~JSON { "name": "Dave", "age" :40, "hats": [ "Cattleman's", "Panama", "Tophat" ] } JSON ruby = JSON.parse(source) ruby # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
Raises an exception if source
is not valid JSON:
# Raises JSON::ParserError (783: unexpected token at ''): JSON.parse('')
Calls
parse(source, opts)
with source
and possibly modified opts
.
Differences from JSON.parse
:
Option max_nesting
, if not provided, defaults to false
, which disables checking for nesting depth.
Option allow_nan
, if not provided, defaults to true
.
Parse a YAML
string in yaml
. Returns the Psych::Nodes::Document
. filename
is used in the exception message if a Psych::SyntaxError
is raised.
Raises a Psych::SyntaxError
when a YAML
syntax error is detected.
Example:
Psych.parse("---\n - a\n - b") # => #<Psych::Nodes::Document:0x00> begin Psych.parse("--- `", filename: "file.txt") rescue Psych::SyntaxError => ex ex.file # => 'file.txt' ex.message # => "(file.txt): found character that cannot start any token" end
See Psych::Nodes
for more information about YAML
AST.
Returns a default parser
Returns true
if the named file has the sticky bit set.
file_name can be an IO
object.
Returns true
if the named files are identical.
file_1 and file_2 can be an IO
object.
open("a", "w") {} p File.identical?("a", "a") #=> true p File.identical?("a", "./a") #=> true File.link("a", "b") p File.identical?("a", "b") #=> true File.symlink("a", "c") p File.identical?("a", "c") #=> true open("d", "w") {} p File.identical?("a", "d") #=> false
Initiates garbage collection, even if manually disabled.
The full_mark
keyword argument determines whether or not to perform a major garbage collection cycle. When set to true
, a major garbage collection cycle is run, meaning all objects are marked. When set to false
, a minor garbage collection cycle is run, meaning only young objects are marked.
The immediate_mark
keyword argument determines whether or not to perform incremental marking. When set to true
, marking is completed during the call to this method. When set to false
, marking is performed in steps that are interleaved with future Ruby
code execution, so marking might not be completed during this method call. Note that if full_mark
is false
, then marking will always be immediate, regardless of the value of immediate_mark
.
The immediate_sweep
keyword argument determines whether or not to defer sweeping (using lazy sweep). When set to false
, sweeping is performed in steps that are interleaved with future Ruby
code execution, so sweeping might not be completed during this method call. When set to true
, sweeping is completed during the call to this method.
Note: These keyword arguments are implementation and version-dependent. They are not guaranteed to be future-compatible and may be ignored if the underlying implementation does not support them.
Prints the amount of time the supplied block takes to run using the debug UI output.
Set
the default parser instance.
Returns a new URI object constructed from the given string uri
:
URI.parse('https://john.doe@www.example.com:123/forum/questions/?tag=networking&order=newest#top') # => #<URI::HTTPS https://john.doe@www.example.com:123/forum/questions/?tag=networking&order=newest#top> URI.parse('http://john.doe@www.example.com:123/forum/questions/?tag=networking&order=newest#top') # => #<URI::HTTP http://john.doe@www.example.com:123/forum/questions/?tag=networking&order=newest#top>
It’s recommended to first ::escape string uri
if it may contain invalid URI
characters.
Mirror the Prism.parse
API by using the serialization API.
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 non-negative number or nil may be used, including Floats to specify fractional seconds. A value of 0 or nil
will execute the block without any timeout. Any negative number will raise an ArgumentError
.
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. However, the block can use ensure to prevent the handling of the exception. For that reason, this method cannot be relied on to enforce timeouts for untrusted blocks.
If a scheduler is defined, it will be used to handle the timeout by invoking Scheduler#timeout_after.
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 non-negative number or nil may be used, including Floats to specify fractional seconds. A value of 0 or nil
will execute the block without any timeout. Any negative number will raise an ArgumentError
.
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. However, the block can use ensure to prevent the handling of the exception. For that reason, this method cannot be relied on to enforce timeouts for untrusted blocks.
If a scheduler is defined, it will be used to handle the timeout by invoking Scheduler#timeout_after.
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()
.
Returns the scheduling priority for specified process, process group, or user.
Argument kind
is one of:
Process::PRIO_PROCESS
: return priority for process.
Process::PRIO_PGRP
: return priority for process group.
Process::PRIO_USER
: return priority for user.
Argument id
is the ID for the process, process group, or user; zero specified the current ID for kind
.
Examples:
Process.getpriority(Process::PRIO_USER, 0) # => 19 Process.getpriority(Process::PRIO_PROCESS, 0) # => 19
Not available on all platforms.
See Process.getpriority
.
Examples:
Process.setpriority(Process::PRIO_USER, 0, 19) # => 0 Process.setpriority(Process::PRIO_PROCESS, 0, 19) # => 0 Process.getpriority(Process::PRIO_USER, 0) # => 19 Process.getpriority(Process::PRIO_PROCESS, 0) # => 19
Not available on all platforms.
Returns a Process::Tms
structure that contains user and system CPU times for the current process, and for its children processes:
Process.times # => #<struct Process::Tms utime=55.122118, stime=35.533068, cutime=0.0, cstime=0.002846>
The precision is platform-defined.