Returns the integer hash value for self
. The value is based on the length, content and encoding of self
.
Related: Object#hash
.
Compares self.downcase
and other_string.downcase
; returns:
-1 if other_string.downcase
is larger.
0 if the two are equal.
1 if other_string.downcase
is smaller.
nil
if the two are incomparable.
Examples:
'foo'.casecmp('foo') # => 0 'foo'.casecmp('food') # => -1 'food'.casecmp('foo') # => 1 'FOO'.casecmp('foo') # => 0 'foo'.casecmp('FOO') # => 0 'foo'.casecmp(1) # => nil
See Case Mapping.
Related: String#casecmp?
.
Returns true
if self
and other_string
are equal after Unicode case folding, otherwise false
:
'foo'.casecmp?('foo') # => true 'foo'.casecmp?('food') # => false 'food'.casecmp?('foo') # => false 'FOO'.casecmp?('foo') # => true 'foo'.casecmp?('FOO') # => true
Returns nil
if the two values are incomparable:
'foo'.casecmp?(1) # => nil
See Case Mapping.
Related: String#casecmp
.
Returns a new String containing other_string
concatenated to self
:
"Hello from " + self.to_s # => "Hello from main"
Returns a new String containing integer
copies of self
:
"Ho! " * 3 # => "Ho! Ho! Ho! " "Ho! " * 0 # => ""
Returns the result of formatting object
into the format specification self
(see Kernel#sprintf
for formatting details):
"%05d" % 123 # => "00123"
If self
contains multiple substitutions, object
must be an Array or Hash containing the values to be substituted:
"%-5s: %016x" % [ "ID", self.object_id ] # => "ID : 00002b054ec93168" "foo = %{foo}" % {foo: 'bar'} # => "foo = bar" "foo = %{foo}, baz = %{baz}" % {foo: 'bar', baz: 'bat'} # => "foo = bar, baz = bat"
Returns the substring of self
specified by the arguments.
When the single Integer argument index
is given, returns the 1-character substring found in self
at offset index
:
'bar'[2] # => "r"
Counts backward from the end of self
if index
is negative:
'foo'[-3] # => "f"
Returns nil
if index
is out of range:
'foo'[3] # => nil 'foo'[-4] # => nil
When the two Integer arguments start
and length
are given, returns the substring of the given length
found in self
at offset start
:
'foo'[0, 2] # => "fo" 'foo'[0, 0] # => ""
Counts backward from the end of self
if start
is negative:
'foo'[-2, 2] # => "oo"
Special case: returns a new empty String if start
is equal to the length of self
:
'foo'[3, 2] # => ""
Returns nil
if start
is out of range:
'foo'[4, 2] # => nil 'foo'[-4, 2] # => nil
Returns the trailing substring of self
if length
is large:
'foo'[1, 50] # => "oo"
Returns nil
if length
is negative:
'foo'[0, -1] # => nil
When the single Range argument range
is given, derives start
and length
values from the given range
, and returns values as above:
'foo'[0..1]
is equivalent to 'foo'[0, 2]
.
'foo'[0...1]
is equivalent to 'foo'[0, 1]
.
When the Regexp argument regexp
is given, and the capture
argument is 0
, returns the first matching substring found in self
, or nil
if none found:
'foo'[/o/] # => "o" 'foo'[/x/] # => nil s = 'hello there' s[/[aeiou](.)\1/] # => "ell" s[/[aeiou](.)\1/, 0] # => "ell"
If argument capture
is given and not 0
, it should be either an Integer capture group index or a String or Symbol capture group name; the method call returns only the specified capture (see Regexp Capturing):
s = 'hello there' s[/[aeiou](.)\1/, 1] # => "l" s[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, "non_vowel"] # => "l" s[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, :vowel] # => "e"
If an invalid capture group index is given, nil
is returned. If an invalid capture group name is given, IndexError
is raised.
When the single String argument substring
is given, returns the substring from self
if found, otherwise nil
:
'foo'['oo'] # => "oo" 'foo'['xx'] # => nil
String#slice
is an alias for String#[]
.
Element Assignment—Replaces some or all of the content of str. The portion of the string affected is determined using the same criteria as String#[]
. If the replacement string is not the same length as the text it is replacing, the string will be adjusted accordingly. If the regular expression or string is used as the index doesn’t match a position in the string, IndexError
is raised. If the regular expression form is used, the optional second Integer
allows you to specify which portion of the match to replace (effectively using the MatchData
indexing rules. The forms that take an Integer
will raise an IndexError
if the value is out of range; the Range
form will raise a RangeError
, and the Regexp
and String
will raise an IndexError
on negative match.
Returns the count of characters (not bytes) in self
:
"\x80\u3042".length # => 2 "hello".length # => 5
String#size
is an alias for String#length
.
Related: String#bytesize
.
Returns the count of bytes in self
:
"\x80\u3042".bytesize # => 4 "hello".bytesize # => 5
Related: String#length
.
Returns true
if the length of self
is zero, false
otherwise:
"hello".empty? # => false " ".empty? # => false "".empty? # => true
Returns the Integer index of the first substring that matches the given regexp
, or nil
if no match found:
'foo' =~ /f/ # => 0 'foo' =~ /o/ # => 1 'foo' =~ /x/ # => nil
Note: also updates Regexp-related global variables.
If the given object
is not a Regexp, returns the value returned by object =~ self
.
Note that string =~ regexp
is different from regexp =~ string
(see Regexp#=~):
number= nil "no. 9" =~ /(?<number>\d+)/ number # => nil (not assigned) /(?<number>\d+)/ =~ "no. 9" number #=> "9"
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 the successor to self
. The successor is calculated by incrementing characters.
The first character to be incremented is the rightmost alphanumeric: or, if no alphanumerics, the rightmost character:
'THX1138'.succ # => "THX1139" '<<koala>>'.succ # => "<<koalb>>" '***'.succ # => '**+'
The successor to a digit is another digit, “carrying” to the next-left character for a “rollover” from 9 to 0, and prepending another digit if necessary:
'00'.succ # => "01" '09'.succ # => "10" '99'.succ # => "100"
The successor to a letter is another letter of the same case, carrying to the next-left character for a rollover, and prepending another same-case letter if necessary:
'aa'.succ # => "ab" 'az'.succ # => "ba" 'zz'.succ # => "aaa" 'AA'.succ # => "AB" 'AZ'.succ # => "BA" 'ZZ'.succ # => "AAA"
The successor to a non-alphanumeric character is the next character in the underlying character set’s collating sequence, carrying to the next-left character for a rollover, and prepending another character if necessary:
s = 0.chr * 3 s # => "\x00\x00\x00" s.succ # => "\x00\x00\x01" s = 255.chr * 3 s # => "\xFF\xFF\xFF" s.succ # => "\x01\x00\x00\x00"
Carrying can occur between and among mixtures of alphanumeric characters:
s = 'zz99zz99' s.succ # => "aaa00aa00" s = '99zz99zz' s.succ # => "100aa00aa"
The successor to an empty String is a new empty String:
''.succ # => ""
String#next
is an alias for String#succ
.
Equivalent to String#succ
, but modifies self
in place; returns self
.
String#next!
is an alias for String#succ!
.
Returns the successor to self
. The successor is calculated by incrementing characters.
The first character to be incremented is the rightmost alphanumeric: or, if no alphanumerics, the rightmost character:
'THX1138'.succ # => "THX1139" '<<koala>>'.succ # => "<<koalb>>" '***'.succ # => '**+'
The successor to a digit is another digit, “carrying” to the next-left character for a “rollover” from 9 to 0, and prepending another digit if necessary:
'00'.succ # => "01" '09'.succ # => "10" '99'.succ # => "100"
The successor to a letter is another letter of the same case, carrying to the next-left character for a rollover, and prepending another same-case letter if necessary:
'aa'.succ # => "ab" 'az'.succ # => "ba" 'zz'.succ # => "aaa" 'AA'.succ # => "AB" 'AZ'.succ # => "BA" 'ZZ'.succ # => "AAA"
The successor to a non-alphanumeric character is the next character in the underlying character set’s collating sequence, carrying to the next-left character for a rollover, and prepending another character if necessary:
s = 0.chr * 3 s # => "\x00\x00\x00" s.succ # => "\x00\x00\x01" s = 255.chr * 3 s # => "\xFF\xFF\xFF" s.succ # => "\x01\x00\x00\x00"
Carrying can occur between and among mixtures of alphanumeric characters:
s = 'zz99zz99' s.succ # => "aaa00aa00" s = '99zz99zz' s.succ # => "100aa00aa"
The successor to an empty String is a new empty String:
''.succ # => ""
String#next
is an alias for String#succ
.
Equivalent to String#succ
, but modifies self
in place; returns self
.
String#next!
is an alias for String#succ!
.
With a block given, calls the block with each String value returned by successive calls to String#succ
; the first value is self
, the next is self.succ
, and so on; the sequence terminates when value other_string
is reached; returns self
:
'a8'.upto('b6') {|s| print s, ' ' } # => "a8"
Output:
a8 a9 b0 b1 b2 b3 b4 b5 b6
If argument exclusive
is given as a truthy object, the last value is omitted:
'a8'.upto('b6', true) {|s| print s, ' ' } # => "a8"
Output:
a8 a9 b0 b1 b2 b3 b4 b5
If other_string
would not be reached, does not call the block:
'25'.upto('5') {|s| fail s } 'aa'.upto('a') {|s| fail s }
With no block given, returns a new Enumerator:
'a8'.upto('b6') # => #<Enumerator: "a8":upto("b6")>
Replaces the contents of self
with the contents of other_string
:
s = 'foo' # => "foo" s.replace('bar') # => "bar"
Returns a string containing the first character of self
:
s = 'foo' # => "foo" s.chr # => "f"
Returns the byte at zero-based index
as an integer:
s = 'abcde' # => "abcde" s.getbyte(0) # => 97 s.getbyte(1) # => 98
Related: String#setbyte
.
Sets the byte at zero-based index
to integer
; returns integer
:
s = 'abcde' # => "abcde" s.setbyte(0, 98) # => 98 s # => "bbcde"
Related: String#getbyte
.
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>