Returns a human-readable description of the underlying method.
"cat".method(:count).inspect #=> "#<Method: String#count(*)>" (1..3).method(:map).inspect #=> "#<Method: Range(Enumerable)#map()>"
In the latter case, the method description includes the “owner” of the original method (Enumerable
module, which is included into Range
).
inspect
also provides, when possible, method argument names (call sequence) and source location.
require 'net/http' Net::HTTP.method(:get).inspect #=> "#<Method: Net::HTTP.get(uri_or_host, path=..., port=...) <skip>/lib/ruby/2.7.0/net/http.rb:457>"
...
in argument definition means argument is optional (has some default value).
For methods defined in C (language core and extensions), location and argument names can’t be extracted, and only generic information is provided in form of *
(any number of arguments) or _
(some positional argument).
"cat".method(:count).inspect #=> "#<Method: String#count(*)>" "cat".method(:+).inspect #=> "#<Method: String#+(_)>""
Dissociates meth from its current receiver. The resulting UnboundMethod
can subsequently be bound to a new object of the same class (see UnboundMethod
).
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 a human-readable description of the underlying method.
"cat".method(:count).inspect #=> "#<Method: String#count(*)>" (1..3).method(:map).inspect #=> "#<Method: Range(Enumerable)#map()>"
In the latter case, the method description includes the “owner” of the original method (Enumerable
module, which is included into Range
).
inspect
also provides, when possible, method argument names (call sequence) and source location.
require 'net/http' Net::HTTP.method(:get).inspect #=> "#<Method: Net::HTTP.get(uri_or_host, path=..., port=...) <skip>/lib/ruby/2.7.0/net/http.rb:457>"
...
in argument definition means argument is optional (has some default value).
For methods defined in C (language core and extensions), location and argument names can’t be extracted, and only generic information is provided in form of *
(any number of arguments) or _
(some positional argument).
"cat".method(:count).inspect #=> "#<Method: String#count(*)>" "cat".method(:+).inspect #=> "#<Method: String#+(_)>""
Bind umeth to obj. If Klass was the class from which umeth was obtained, obj.kind_of?(Klass)
must be true.
class A def test puts "In test, class = #{self.class}" end end class B < A end class C < B end um = B.instance_method(:test) bm = um.bind(C.new) bm.call bm = um.bind(B.new) bm.call bm = um.bind(A.new) bm.call
produces:
In test, class = C In test, class = B prog.rb:16:in `bind': bind argument must be an instance of B (TypeError) from prog.rb:16
returns main ractor
return true if the current ractor is main ractor
Returns an array of all existing Thread
objects that belong to this group.
ThreadGroup::Default.list #=> [#<Thread:0x401bdf4c run>]
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 main thread.
Stops execution of the current thread, putting it into a “sleep” state, and schedules execution of another thread.
a = Thread.new { print "a"; Thread.stop; print "c" } sleep 0.1 while a.status!='sleep' print "b" a.run a.join #=> "abc"
Returns an array of Thread
objects for all threads that are either runnable or stopped.
Thread.new { sleep(200) } Thread.new { 1000000.times {|i| i*i } } Thread.new { Thread.stop } Thread.list.each {|t| p t}
This will produce:
#<Thread:0x401b3e84 sleep> #<Thread:0x401b3f38 run> #<Thread:0x401b3fb0 sleep> #<Thread:0x401bdf4c run>
The calling thread will suspend execution and run this thr
.
Does not return until thr
exits or until the given limit
seconds have passed.
If the time limit expires, nil
will be returned, otherwise thr
is returned.
Any threads not joined will be killed when the main program exits.
If thr
had previously raised an exception and the ::abort_on_exception
or $DEBUG flags are not set, (so the exception has not yet been processed), it will be processed at this time.
a = Thread.new { print "a"; sleep(10); print "b"; print "c" } x = Thread.new { print "x"; Thread.pass; print "y"; print "z" } x.join # Let thread x finish, thread a will be killed on exit. #=> "axyz"
The following example illustrates the limit
parameter.
y = Thread.new { 4.times { sleep 0.1; puts 'tick... ' }} puts "Waiting" until y.join(0.15)
This will produce:
tick... Waiting tick... Waiting tick... tick...
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.
Returns the status of thr
.
"sleep"
Returned if this thread is sleeping or waiting on I/O
"run"
When this thread is executing
"aborting"
If this thread is aborting
false
When this thread is terminated normally
nil
If terminated with an exception.
a = Thread.new { raise("die now") } b = Thread.new { Thread.stop } c = Thread.new { Thread.exit } d = Thread.new { sleep } d.kill #=> #<Thread:0x401b3678 aborting> a.status #=> nil b.status #=> "sleep" c.status #=> false d.status #=> "aborting" Thread.current.status #=> "run"
Returns true
if thr
is dead or sleeping.
a = Thread.new { Thread.stop } b = Thread.current a.stop? #=> true b.stop? #=> false
Returns the current backtrace of the target thread.
Dump the name, id, and status of thr to a string.
Returns a string containing a human-readable TracePoint
status.
Returns internal information of TracePoint
.
The contents of the returned value are implementation-specific and may change in the future.
This method is only for debugging TracePoint
itself.
A convenience method for TracePoint.new
that activates the trace automatically.
trace = TracePoint.trace(:call) { |tp| [tp.lineno, tp.event] } #=> #<TracePoint:enabled> trace.enabled? #=> true
Returns the line number of the event.
Performs a test on one or both of the filesystem entities at the given paths path0
and path1
:
Each path path0
or path1
points to a file, directory, device, pipe, etc.
Character char
selects a specific test.
The tests:
Each of these tests operates only on the entity at path0
, and returns true
or false
; for a non-existent entity, returns false
(does not raise exception):
Character | Test |
---|---|
'b' |
Whether the entity is a block device. |
'c' |
Whether the entity is a character device. |
'd' |
Whether the entity is a directory. |
'e' |
Whether the entity is an existing entity. |
'f' |
Whether the entity is an existing regular file. |
'g' |
Whether the entity's setgid bit is set. |
'G' |
Whether the entity's group ownership is equal to the caller's. |
'k' |
Whether the entity's sticky bit is set. |
'l' |
Whether the entity is a symbolic link. |
'o' |
Whether the entity is owned by the caller's effective uid. |
'O' |
Like 'o' , but uses the real uid (not the effective uid). |
'p' |
Whether the entity is a FIFO device (named pipe). |
'r' |
Whether the entity is readable by the caller's effective uid/gid. |
'R' |
Like 'r' , but uses the real uid/gid (not the effective uid/gid). |
'S' |
Whether the entity is a socket. |
'u' |
Whether the entity's setuid bit is set. |
'w' |
Whether the entity is writable by the caller's effective uid/gid. |
'W' |
Like 'w' , but uses the real uid/gid (not the effective uid/gid). |
'x' |
Whether the entity is executable by the caller's effective uid/gid. |
'X' |
Like 'x' , but uses the real uid/gid (not the effective uid/git). |
'z' |
Whether the entity exists and is of length zero. |
This test operates only on the entity at path0
, and returns an integer size or nil
:
Character | Test |
---|---|
's' |
Returns positive integer size if the entity exists and has non-zero length, nil otherwise. |
Each of these tests operates only on the entity at path0
, and returns a Time
object; raises an exception if the entity does not exist:
Character | Test |
---|---|
'A' |
Last access time for the entity. |
'C' |
Last change time for the entity. |
'M' |
Last modification time for the entity. |
Each of these tests operates on the modification time (mtime
) of each of the entities at path0
and path1
, and returns a true
or false
; returns false
if either entity does not exist:
Character | Test |
---|---|
'<' |
Whether the 'mtime` at `path0` is less than that at `path1`. |
'=' |
Whether the 'mtime` at `path0` is equal to that at `path1`. |
'>' |
Whether the 'mtime` at `path0` is greater than that at `path1`. |
This test operates on the content of each of the entities at path0
and path1
, and returns a true
or false
; returns false
if either entity does not exist:
Character | Test |
---|---|
'-' |
Whether the entities exist and are identical. |
Equivalent to method Kernel#gets
, except that it raises an exception if called at end-of-stream:
$ cat t.txt | ruby -e "p readlines; readline" ["First line\n", "Second line\n", "\n", "Fourth line\n", "Fifth line\n"] in `readline': end of file reached (EOFError)
Optional keyword argument chomp
specifies whether line separators are to be omitted.