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.
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>
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
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.
Returns a new Complex object if the arguments are valid; otherwise raises an exception if exception
is true
; otherwise returns nil
.
With Numeric
arguments real
and imag
, returns Complex.rect(real, imag)
if the arguments are valid.
With string argument s
, returns a new Complex object if the argument is valid; the string may have:
One or two numeric substrings, each of which specifies a Complex
, Float
, Integer
, Numeric
, or Rational
value, specifying rectangular coordinates:
Sign-separated real and imaginary numeric substrings (with trailing character 'i'
):
Complex('1+2i') # => (1+2i) Complex('+1+2i') # => (1+2i) Complex('+1-2i') # => (1-2i) Complex('-1+2i') # => (-1+2i) Complex('-1-2i') # => (-1-2i)
Real-only numeric string (without trailing character 'i'
):
Complex('1') # => (1+0i) Complex('+1') # => (1+0i) Complex('-1') # => (-1+0i)
Imaginary-only numeric string (with trailing character 'i'
):
Complex('1i') # => (0+1i) Complex('+1i') # => (0+1i) Complex('-1i') # => (0-1i)
At-sign separated real and imaginary rational substrings, each of which specifies a Rational
value, specifying polar coordinates:
Complex('1/2@3/4') # => (0.36584443443691045+0.34081938001166706i) Complex('+1/2@+3/4') # => (0.36584443443691045+0.34081938001166706i) Complex('+1/2@-3/4') # => (0.36584443443691045-0.34081938001166706i) Complex('-1/2@+3/4') # => (-0.36584443443691045-0.34081938001166706i) Complex('-1/2@-3/4') # => (-0.36584443443691045+0.34081938001166706i)
If object is string-like, parse the string and return the parsed result as a Ruby
data structure. Otherwise, generate a JSON
text from the Ruby
data structure object and return it.
The opts argument is passed through to generate/parse respectively. See generate and parse for their documentation.
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. |
Produces a shallow copy of obj—the instance variables of obj are copied, but not the objects they reference. clone
copies the frozen value state of obj, unless the :freeze
keyword argument is given with a false or true value. See also the discussion under Object#dup
.
class Klass attr_accessor :str end s1 = Klass.new #=> #<Klass:0x401b3a38> s1.str = "Hello" #=> "Hello" s2 = s1.clone #=> #<Klass:0x401b3998 @str="Hello"> s2.str[1,4] = "i" #=> "i" s1.inspect #=> "#<Klass:0x401b3a38 @str=\"Hi\">" s2.inspect #=> "#<Klass:0x401b3998 @str=\"Hi\">"
This method may have class-specific behavior. If so, that behavior will be documented under the #initialize_copy
method of the class.
Returns a string converted from object
.
Tries to convert object
to a string using to_str
first and to_s
second:
String([0, 1, 2]) # => "[0, 1, 2]" String(0..5) # => "0..5" String({foo: 0, bar: 1}) # => "{foo: 0, bar: 1}"
Raises TypeError
if object
cannot be converted to a string.
Returns x/y
or arg
as a Rational
.
Rational(2, 3) #=> (2/3) Rational(5) #=> (5/1) Rational(0.5) #=> (1/2) Rational(0.3) #=> (5404319552844595/18014398509481984) Rational("2/3") #=> (2/3) Rational("0.3") #=> (3/10) Rational("10 cents") #=> ArgumentError Rational(nil) #=> TypeError Rational(1, nil) #=> TypeError Rational("10 cents", exception: false) #=> nil
Syntax of the string form:
string form = extra spaces , rational , extra spaces ; rational = [ sign ] , unsigned rational ; unsigned rational = numerator | numerator , "/" , denominator ; numerator = integer part | fractional part | integer part , fractional part ; denominator = digits ; integer part = digits ; fractional part = "." , digits , [ ( "e" | "E" ) , [ sign ] , digits ] ; sign = "-" | "+" ; digits = digit , { digit | "_" , digit } ; digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ; extra spaces = ? \s* ? ;
See also String#to_r
.
Returns the count of elements, based on an argument or block criterion, if given.
With no argument and no block given, returns the number of elements:
[0, 1, 2].count # => 3 {foo: 0, bar: 1, baz: 2}.count # => 3
With argument object
given, returns the number of elements that are ==
to object
:
[0, 1, 2, 1].count(1) # => 2
With a block given, calls the block with each element and returns the number of elements for which the block returns a truthy value:
[0, 1, 2, 3].count {|element| element < 2} # => 2 {foo: 0, bar: 1, baz: 2}.count {|key, value| value < 2} # => 2
Returns an array of objects returned by the block.
With a block given, calls the block with successive elements; returns an array of the objects returned by the block:
(0..4).map {|i| i*i } # => [0, 1, 4, 9, 16] {foo: 0, bar: 1, baz: 2}.map {|key, value| value*2} # => [0, 2, 4]
With no block given, returns an Enumerator
.
With a block given, returns an array of two arrays:
The first having those elements for which the block returns a truthy value.
The other having all other elements.
Examples:
p = (1..4).partition {|i| i.even? } p # => [[2, 4], [1, 3]] p = ('a'..'d').partition {|c| c < 'c' } p # => [["a", "b"], ["c", "d"]] h = {foo: 0, bar: 1, baz: 2, bat: 3} p = h.partition {|key, value| key.start_with?('b') } p # => [[[:bar, 1], [:baz, 2], [:bat, 3]], [[:foo, 0]]] p = h.partition {|key, value| value < 2 } p # => [[[:foo, 0], [:bar, 1]], [[:baz, 2], [:bat, 3]]]
With no block given, returns an Enumerator
.
Related: Enumerable#group_by
.
Returns the first element or elements.
With no argument, returns the first element, or nil
if there is none:
(1..4).first # => 1 %w[a b c].first # => "a" {foo: 1, bar: 1, baz: 2}.first # => [:foo, 1] [].first # => nil
With integer argument n
, returns an array containing the first n
elements that exist:
(1..4).first(2) # => [1, 2] %w[a b c d].first(3) # => ["a", "b", "c"] %w[a b c d].first(50) # => ["a", "b", "c", "d"] {foo: 1, bar: 1, baz: 2}.first(2) # => [[:foo, 1], [:bar, 1]] [].first(2) # => []
Returns whether exactly one element meets a given criterion.
With no argument and no block, returns whether exactly one element is truthy:
(1..1).one? # => true [1, nil, false].one? # => true (1..4).one? # => false {foo: 0}.one? # => true {foo: 0, bar: 1}.one? # => false [].one? # => false
With argument pattern
and no block, returns whether for exactly one element element
, pattern === element
:
[nil, false, 0].one?(Integer) # => true [nil, false, 0].one?(Numeric) # => true [nil, false, 0].one?(Float) # => false %w[bar baz bat bam].one?(/m/) # => true %w[bar baz bat bam].one?(/foo/) # => false %w[bar baz bat bam].one?('ba') # => false {foo: 0, bar: 1, baz: 2}.one?(Array) # => false {foo: 0}.one?(Array) # => true [].one?(Integer) # => false
With a block given, returns whether the block returns a truthy value for exactly one element:
(1..4).one? {|element| element < 2 } # => true (1..4).one? {|element| element < 1 } # => false {foo: 0, bar: 1, baz: 2}.one? {|key, value| value < 1 } # => true {foo: 0, bar: 1, baz: 2}.one? {|key, value| value < 2 } # => false
Returns whether no element meets a given criterion.
With no argument and no block, returns whether no element is truthy:
(1..4).none? # => false [nil, false].none? # => true {foo: 0}.none? # => false {foo: 0, bar: 1}.none? # => false [].none? # => true
With argument pattern
and no block, returns whether for no element element
, pattern === element
:
[nil, false, 1.1].none?(Integer) # => true %w[bar baz bat bam].none?(/m/) # => false %w[bar baz bat bam].none?(/foo/) # => true %w[bar baz bat bam].none?('ba') # => true {foo: 0, bar: 1, baz: 2}.none?(Hash) # => true {foo: 0}.none?(Array) # => false [].none?(Integer) # => true
With a block given, returns whether the block returns a truthy value for no element:
(1..4).none? {|element| element < 1 } # => true (1..4).none? {|element| element < 2 } # => false {foo: 0, bar: 1, baz: 2}.none? {|key, value| value < 0 } # => true {foo: 0, bar: 1, baz: 2}.none? {|key, value| value < 1 } # => false
Returns an array of all non-nil
elements:
a = [nil, 0, nil, 'a', false, nil, false, nil, 'a', nil, 0, nil] a.compact # => [0, "a", false, false, "a", 0]
Enables the coverage measurement. See the documentation of Coverage
class in detail. This is equivalent to Coverage.setup
and Coverage.resume
.
Returns the state of the coverage measurement.
Generates a hex-encoded version of a given string.
Returns system temporary directory; typically “/tmp”.