Should be implemented by a extended class.
tsort_each_child
is used to iterate for child nodes of node.
See Zlib::GzipReader
documentation for a description.
Iterate all direct child instruction sequences. Iteration order is implementation/version defined so that people should not rely on the order.
Checks the scheme v
component against the URI::Parser
Regexp
for :SCHEME.
Verifies each certificate in chain
has signed the following certificate and is valid for the given time
.
Returns the element at offset index
.
With the single Integer argument index
, returns the element at offset index
:
a = [:foo, 'bar', 2] a.fetch(1) # => "bar"
If index
is negative, counts from the end of the array:
a = [:foo, 'bar', 2] a.fetch(-1) # => 2 a.fetch(-2) # => "bar"
With arguments index
and default_value
, returns the element at offset index
if index is in range, otherwise returns default_value
:
a = [:foo, 'bar', 2] a.fetch(1, nil) # => "bar"
With argument index
and a block, returns the element at offset index
if index is in range (and the block is not called); otherwise calls the block with index and returns its return value:
a = [:foo, 'bar', 2] a.fetch(1) {|index| raise 'Cannot happen' } # => "bar" a.fetch(50) {|index| "Value for #{index}" } # => "Value for 50"
Iterates over array elements.
When a block given, passes each successive array element to the block; returns self
:
a = [:foo, 'bar', 2] a.each {|element| puts "#{element.class} #{element}" }
Output:
Symbol foo String bar Integer 2
Allows the array to be modified during iteration:
a = [:foo, 'bar', 2] a.each {|element| puts element; a.clear if element.to_s.start_with?('b') }
Output:
foo bar
When no block given, returns a new Enumerator:
a = [:foo, 'bar', 2] e = a.each e # => #<Enumerator: [:foo, "bar", 2]:each> a1 = e.each {|element| puts "#{element.class} #{element}" }
Output:
Symbol foo String bar Integer 2
Related: each_index
, reverse_each
.
When called with positive Integer argument count
and a block, calls the block with each element, then does so again, until it has done so count
times; returns nil
:
output = [] [0, 1].cycle(2) {|element| output.push(element) } # => nil output # => [0, 1, 0, 1]
If count
is zero or negative, does not call the block:
[0, 1].cycle(0) {|element| fail 'Cannot happen' } # => nil [0, 1].cycle(-1) {|element| fail 'Cannot happen' } # => nil
When a block is given, and argument is omitted or nil
, cycles forever:
# Prints 0 and 1 forever. [0, 1].cycle {|element| puts element } [0, 1].cycle(nil) {|element| puts element }
When no block is given, returns a new Enumerator:
[0, 1].cycle(2) # => #<Enumerator: [0, 1]:cycle(2)> [0, 1].cycle # => # => #<Enumerator: [0, 1]:cycle> [0, 1].cycle.first(5) # => [0, 1, 0, 1, 0]
Returns an element from self
selected by a binary search. self
should be sorted, but this is not checked.
By using binary search, finds a value from this array which meets the given condition in O(log n)
where n
is the size of the array.
There are two search modes:
Find-minimum mode: the block should return true
or false
.
Find-any mode: the block should return a numeric value.
The block should not mix the modes by and sometimes returning true
or false
and sometimes returning a numeric value, but this is not checked.
Find-Minimum Mode
In find-minimum mode, the block always returns true
or false
. The further requirement (though not checked) is that there are no indexes i
and j
such that:
0 <= i < j <= self.size
.
The block returns true
for self[i]
and false
for self[j]
.
In find-minimum mode, method bsearch returns the first element for which the block returns true.
Examples:
a = [0, 4, 7, 10, 12] a.bsearch {|x| x >= 4 } # => 4 a.bsearch {|x| x >= 6 } # => 7 a.bsearch {|x| x >= -1 } # => 0 a.bsearch {|x| x >= 100 } # => nil
Less formally: the block is such that all false
-evaluating elements precede all true
-evaluating elements.
These make sense as blocks in find-minimum mode:
a = [0, 4, 7, 10, 12] a.map {|x| x >= 4 } # => [false, true, true, true, true] a.map {|x| x >= 6 } # => [false, false, true, true, true] a.map {|x| x >= -1 } # => [true, true, true, true, true] a.map {|x| x >= 100 } # => [false, false, false, false, false]
This would not make sense:
a = [0, 4, 7, 10, 12] a.map {|x| x == 7 } # => [false, false, true, false, false]
Find-Any Mode
In find-any mode, the block always returns a numeric value. The further requirement (though not checked) is that there are no indexes i
and j
such that:
0 <= i < j <= self.size
.
The block returns a negative value for self[i]
and a positive value for self[j]
.
The block returns a negative value for self[i]
and zero self[j]
.
The block returns zero for self[i]
and a positive value for self[j]
.
In find-any mode, method bsearch returns some element for which the block returns zero, or nil
if no such element is found.
Examples:
a = [0, 4, 7, 10, 12] a.bsearch {|element| 7 <=> element } # => 7 a.bsearch {|element| -1 <=> element } # => nil a.bsearch {|element| 5 <=> element } # => nil a.bsearch {|element| 15 <=> element } # => nil
Less formally: the block is such that:
All positive-evaluating elements precede all zero-evaluating elements.
All positive-evaluating elements precede all negative-evaluating elements.
All zero-evaluating elements precede all negative-evaluating elements.
These make sense as blocks in find-any mode:
a = [0, 4, 7, 10, 12] a.map {|element| 7 <=> element } # => [1, 1, 0, -1, -1] a.map {|element| -1 <=> element } # => [-1, -1, -1, -1, -1] a.map {|element| 5 <=> element } # => [1, 1, -1, -1, -1] a.map {|element| 15 <=> element } # => [1, 1, 1, 1, 1]
This would not make sense:
a = [0, 4, 7, 10, 12] a.map {|element| element <=> 7 } # => [-1, -1, 0, 1, 1]
Returns an enumerator if no block given:
a = [0, 4, 7, 10, 12] a.bsearch # => #<Enumerator: [0, 4, 7, 10, 12]:bsearch>
Returns a string containing the character represented by the int
‘s value according to encoding
.
65.chr #=> "A" 230.chr #=> "\xE6" 255.chr(Encoding::UTF_8) #=> "\u00FF"
Returns a Matchdata object (or nil
) based on self
and the given pattern
.
Note: also updates Regexp-related global variables.
Computes regexp
by converting pattern
(if not already a Regexp).
regexp = Regexp.new(pattern)
Computes matchdata
, which will be either a MatchData object or nil
(see Regexp#match
):
matchdata = <tt>regexp.match(self)
With no block given, returns the computed matchdata
:
'foo'.match('f') # => #<MatchData "f"> 'foo'.match('o') # => #<MatchData "o"> 'foo'.match('x') # => nil
If Integer argument offset
is given, the search begins at index offset
:
'foo'.match('f', 1) # => nil 'foo'.match('o', 1) # => #<MatchData "o">
With a block given, calls the block with the computed matchdata
and returns the block’s return value:
'foo'.match(/o/) {|matchdata| matchdata } # => #<MatchData "o"> 'foo'.match(/x/) {|matchdata| matchdata } # => nil 'foo'.match(/f/, 1) {|matchdata| matchdata } # => nil
Returns true
or false
based on whether a match is found for self
and pattern
.
Note: does not update Regexp-related global variables.
Computes regexp
by converting pattern
(if not already a Regexp).
regexp = Regexp.new(pattern)
Returns true
if self+.match(regexp)
returns a Matchdata object, false
otherwise:
'foo'.match?(/o/) # => true 'foo'.match?('o') # => true 'foo'.match?(/x/) # => false
If Integer argument offset
is given, the search begins at index offset
:
'foo'.match?('f', 1) # => false 'foo'.match?('o', 1) # => true
Returns an array of characters in str. This is a shorthand for str.each_char.to_a
.
If a block is given, which is a deprecated form, works the same as each_char
.
Returns a new String
with the last character removed. If the string ends with \r\n
, both characters are removed. Applying chop
to an empty string returns an empty string. String#chomp
is often a safer alternative, as it leaves the string unchanged if it doesn’t end in a record separator.
"string\r\n".chop #=> "string" "string\n\r".chop #=> "string\n" "string\n".chop #=> "string" "string".chop #=> "strin" "x".chop.chop #=> ""
Returns a new String
with the given record separator removed from the end of str (if present). If $/
has not been changed from the default Ruby record separator, then chomp
also removes carriage return characters (that is it will remove \n
, \r
, and \r\n
). If $/
is an empty string, it will remove all trailing newlines from the string.
"hello".chomp #=> "hello" "hello\n".chomp #=> "hello" "hello\r\n".chomp #=> "hello" "hello\n\r".chomp #=> "hello\n" "hello\r".chomp #=> "hello" "hello \n there".chomp #=> "hello \n there" "hello".chomp("llo") #=> "he" "hello\r\n\r\n".chomp('') #=> "hello" "hello\r\n\r\r\n".chomp('') #=> "hello\r\n\r"
Processes str as for String#chop
, returning str, or nil
if str is the empty string. See also String#chomp!
.
Modifies str in place as described for String#chomp
, returning str, or nil
if no modifications were made.
Fiber
scheduler, set in the current thread with Fiber.set_scheduler
. If the scheduler is nil
(which is the default), non-blocking fibers behavior is the same as blocking. (see “Non-blocking fibers” section in class docs for details about the scheduler concept).
The method is expected to immediately run the provided block of code in a separate non-blocking fiber.
puts "Go to sleep!" Fiber.set_scheduler(MyScheduler.new) Fiber.schedule do puts "Going to sleep" sleep(1) puts "I slept well" end puts "Wakey-wakey, sleepyhead"
Assuming MyScheduler is properly implemented, this program will produce:
Go to sleep! Going to sleep Wakey-wakey, sleepyhead ...1 sec pause here... I slept well
…e.g. on the first blocking operation inside the Fiber
(sleep(1)
), the control is yielded at the outside code (main fiber), and at the end of the execution, the scheduler takes care of properly resuming all the blocked fibers.
Note that the behavior described above is how the method is expected to behave, actual behavior is up to the current scheduler’s implementation of Fiber::SchedulerInterface#fiber
method. Ruby doesn’t enforce this method to behave in any particular way.
If the scheduler is not set, the method raises RuntimeError (No scheduler is available!)
.
Calls the block once for each entry in the named directory, passing the filename of each entry as a parameter to the block.
If no block is given, an enumerator is returned instead.
Dir.foreach("testdir") {|x| puts "Got #{x}" }
produces:
Got . Got .. Got config.h Got main.rb