Returns true
if object
has the same length and content; as self
; false
otherwise:
s = 'foo' s.eql?('foo') # => true s.eql?('food') # => false s.eql?('FOO') # => false
Returns false
if the two strings’ encodings are not compatible:
"\u{e4 f6 fc}".encode("ISO-8859-1").eql?("\u{c4 d6 dc}") # => false
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. See examples at String Slices.
Replaces all, some, or none of the contents of self
; returns new_string
. See String Slices.
A few examples:
s = 'foo' s[2] = 'rtune' # => "rtune" s # => "fortune" s[1, 5] = 'init' # => "init" s # => "finite" s[3..4] = 'al' # => "al" s # => "finale" s[/e$/] = 'ly' # => "ly" s # => "finally" s['lly'] = 'ncial' # => "ncial" s # => "financial"
Returns the count of characters (not bytes) in self
:
'foo'.length # => 3 'тест'.length # => 4 'こんにちは'.length # => 5
Contrast with String#bytesize
:
'foo'.bytesize # => 3 'тест'.bytesize # => 8 'こんにちは'.bytesize # => 15
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 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 Global Variables at Regexp
.
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 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 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 # => ""
Equivalent to String#succ
, but modifies self
in place; returns self
.
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 # => ""
Equivalent to String#succ
, but modifies self
in place; returns self
.
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, or nil
if index
is out of range:
s = 'abcde' # => "abcde" s.getbyte(0) # => 97 s.getbyte(-1) # => 101 s.getbyte(5) # => nil
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
.