if /foo #{bar}/ then end
^^^^^^^^^^^^
With a block given, sorts the elements of self
in place; returns self.
Calls the block with each successive element; sorts elements based on the values returned from the block:
a = ['aaaa', 'bbb', 'cc', 'd'] a.sort_by! {|element| element.size } a # => ["d", "cc", "bbb", "aaaa"]
For duplicate values returned by the block, the ordering is indeterminate, and may be unstable.
With no block given, returns a new Enumerator
.
Related: see Methods for Assigning.
Returns the count of bytes (not characters) in self
:
'foo'.bytesize # => 3 'тест'.bytesize # => 8 'こんにちは'.bytesize # => 15
Contrast with String#length
:
'foo'.length # => 3 'тест'.length # => 4 'こんにちは'.length # => 5
Returns the Integer
byte-based index of the first occurrence of the given substring
, or nil
if none found:
'foo'.byteindex('f') # => 0 'foo'.byteindex('o') # => 1 'foo'.byteindex('oo') # => 1 'foo'.byteindex('ooo') # => nil
Returns the Integer
byte-based index of the first match for the given Regexp
regexp
, or nil
if none found:
'foo'.byteindex(/f/) # => 0 'foo'.byteindex(/o/) # => 1 'foo'.byteindex(/oo/) # => 1 'foo'.byteindex(/ooo/) # => nil
Integer
argument offset
, if given, specifies the byte-based position in the string to begin the search:
'foo'.byteindex('o', 1) # => 1 'foo'.byteindex('o', 2) # => 2 'foo'.byteindex('o', 3) # => nil
If offset
is negative, counts backward from the end of self
:
'foo'.byteindex('o', -1) # => 2 'foo'.byteindex('o', -2) # => 1 'foo'.byteindex('o', -3) # => 1 'foo'.byteindex('o', -4) # => nil
If offset
does not land on character (codepoint) boundary, IndexError
is raised.
Related: String#index
, String#byterindex
.
Returns the Integer
byte-based index of the last occurrence of the given substring
, or nil
if none found:
'foo'.byterindex('f') # => 0 'foo'.byterindex('o') # => 2 'foo'.byterindex('oo') # => 1 'foo'.byterindex('ooo') # => nil
Returns the Integer
byte-based index of the last match for the given Regexp
regexp
, or nil
if none found:
'foo'.byterindex(/f/) # => 0 'foo'.byterindex(/o/) # => 2 'foo'.byterindex(/oo/) # => 1 'foo'.byterindex(/ooo/) # => nil
The last match means starting at the possible last position, not the last of longest matches.
'foo'.byterindex(/o+/) # => 2 $~ #=> #<MatchData "o">
To get the last longest match, needs to combine with negative lookbehind.
'foo'.byterindex(/(?<!o)o+/) # => 1 $~ #=> #<MatchData "oo">
Or String#byteindex
with negative lookforward.
'foo'.byteindex(/o+(?!.*o)/) # => 1 $~ #=> #<MatchData "oo">
Integer
argument offset
, if given and non-negative, specifies the maximum starting byte-based position in the string to end the search:
'foo'.byterindex('o', 0) # => nil 'foo'.byterindex('o', 1) # => 1 'foo'.byterindex('o', 2) # => 2 'foo'.byterindex('o', 3) # => 2
If offset
is a negative Integer
, the maximum starting position in the string to end the search is the sum of the string’s length and offset
:
'foo'.byterindex('o', -1) # => 2 'foo'.byterindex('o', -2) # => 1 'foo'.byterindex('o', -3) # => nil 'foo'.byterindex('o', -4) # => nil
If offset
does not land on character (codepoint) boundary, IndexError
is raised.
Related: String#byteindex
.
Returns a substring of self
, or nil
if the substring cannot be constructed.
With integer arguments index
and length
given, returns the substring beginning at the given index
of the given length
(if possible), or nil
if length
is negative or index
falls outside of self
:
s = '0123456789' # => "0123456789" s.byteslice(2) # => "2" s.byteslice(200) # => nil s.byteslice(4, 3) # => "456" s.byteslice(4, 30) # => "456789" s.byteslice(4, -1) # => nil s.byteslice(40, 2) # => nil
In either case above, counts backwards from the end of self
if index
is negative:
s = '0123456789' # => "0123456789" s.byteslice(-4) # => "6" s.byteslice(-4, 3) # => "678"
With Range
argument range
given, returns byteslice(range.begin, range.size)
:
s = '0123456789' # => "0123456789" s.byteslice(4..6) # => "456" s.byteslice(-6..-4) # => "456" s.byteslice(5..2) # => "" # range.size is zero. s.byteslice(40..42) # => nil
In all cases, a returned string has the same encoding as self
:
s.encoding # => #<Encoding:UTF-8> s.byteslice(4).encoding # => #<Encoding:UTF-8>
Replaces some or all of the content of self
with str
, and returns self
. The portion of the string affected is determined using the same criteria as String#byteslice
, except that length
cannot be omitted. If the replacement string is not the same length as the text it is replacing, the string will be adjusted accordingly.
If str_index
and str_length
, or str_range
are given, the content of self
is replaced by str.byteslice(str_index, str_length) or str.byteslice(str_range); however the substring of str
is not allocated as a new string.
The form that take an Integer
will raise an IndexError
if the value is out of range; the Range
form will raise a RangeError
. If the beginning or ending offset does not land on character (codepoint) boundary, an IndexError
will be raised.