Helper method for grabbing elements from document
Like ‘take_while` except when it stops iterating, it also returns the line that caused it to stop
Returns the source encoding name as a string.
Returns the destination encoding name as a string.
Returns the source encoding name as a string.
Returns the destination encoding name as a string.
The line number in the source code where this AST’s text began.
The column number in the source code where this AST’s text began.
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
.
Returns a 1-character string containing the character represented by the value of self
, according to the given encoding
.
65.chr # => "A" 0.chr # => "\x00" 255.chr # => "\xFF" string = 255.chr(Encoding::UTF_8) string.encoding # => Encoding::UTF_8
Raises an exception if self
is negative.
Related: Integer#ord
.
Returns a MatchData
object (or nil
) based on self
and the given pattern
.
Note: also updates Global Variables at Regexp
.
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 Global Variables at Regexp
.
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 a string containing the first character of self
:
s = 'foo' # => "foo" s.chr # => "f"
Returns an array of the characters in self
:
'hello'.chars # => ["h", "e", "l", "l", "o"] 'тест'.chars # => ["т", "е", "с", "т"] 'こんにちは'.chars # => ["こ", "ん", "に", "ち", "は"]
Returns a new string copied from self
, with trailing characters possibly removed.
Removes "\r\n"
if those are the last two characters.
"abc\r\n".chop # => "abc" "тест\r\n".chop # => "тест" "こんにちは\r\n".chop # => "こんにちは"
Otherwise removes the last character if it exists.
'abcd'.chop # => "abc" 'тест'.chop # => "тес" 'こんにちは'.chop # => "こんにち" ''.chop # => ""
If you only need to remove the newline separator at the end of the string, String#chomp
is a better alternative.
Returns a new string copied from self
, with trailing characters possibly removed:
When line_sep
is "\n"
, removes the last one or two characters if they are "\r"
, "\n"
, or "\r\n"
(but not "\n\r"
):
$/ # => "\n" "abc\r".chomp # => "abc" "abc\n".chomp # => "abc" "abc\r\n".chomp # => "abc" "abc\n\r".chomp # => "abc\n" "тест\r\n".chomp # => "тест" "こんにちは\r\n".chomp # => "こんにちは"
When line_sep
is ''
(an empty string), removes multiple trailing occurrences of "\n"
or "\r\n"
(but not "\r"
or "\n\r"
):
"abc\n\n\n".chomp('') # => "abc" "abc\r\n\r\n\r\n".chomp('') # => "abc" "abc\n\n\r\n\r\n\n\n".chomp('') # => "abc" "abc\n\r\n\r\n\r".chomp('') # => "abc\n\r\n\r\n\r" "abc\r\r\r".chomp('') # => "abc\r\r\r"
When line_sep
is neither "\n"
nor ''
, removes a single trailing line separator if there is one:
'abcd'.chomp('d') # => "abc" 'abcdd'.chomp('d') # => "abcd"
Like String#chop
, but modifies self
in place; returns nil
if self
is empty, self
otherwise.
Related: String#chomp!
.
Like String#chomp
, but modifies self
in place; returns nil
if no modification made, self
otherwise.
Returns the Fiber
scheduler, that was last set for the current thread with Fiber.set_scheduler
. Returns nil
if no scheduler is set (which is the default), and non-blocking fibers’ behavior is the same as blocking. (see “Non-blocking fibers” section in class docs for details about the scheduler concept).