Returns the value associated with the given key
if found.
If key
is not found, returns nil
.
Returns the string field value for the case-insensitive field key
, or nil
if there is no such key; see Fields:
res = Net::HTTP.get_response(hostname, '/todos/1') res['Connection'] # => "keep-alive" res['Nosuch'] # => nil
Note that some field values may be retrieved via convenience methods; see Getters.
Inserts the given other_string
into self
; returns self
.
If the Integer
index
is positive, inserts other_string
at offset index
:
'foo'.insert(1, 'bar') # => "fbaroo"
If the Integer
index
is negative, counts backward from the end of self
and inserts other_string
at offset index+1
(that is, after self[index]
):
'foo'.insert(-2, 'bar') # => "fobaro"
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 integer index of the first match for the given argument, or nil
if none found; the search of self
is forward, and begins at position offset
(in characters).
With string argument substring
, returns the index of the first matching substring in self
:
'foo'.index('f') # => 0 'foo'.index('o') # => 1 'foo'.index('oo') # => 1 'foo'.index('ooo') # => nil 'тест'.index('с') # => 2 'こんにちは'.index('ち') # => 3
With Regexp
argument regexp
, returns the index of the first match in self
:
'foo'.index(/o./) # => 1 'foo'.index(/.o/) # => 0
With positive integer offset
, begins the search at position offset
:
'foo'.index('o', 1) # => 1 'foo'.index('o', 2) # => 2 'foo'.index('o', 3) # => nil 'тест'.index('с', 1) # => 2 'こんにちは'.index('ち', 2) # => 3
With negative integer offset
, selects the search position by counting backward from the end of self
:
'foo'.index('o', -1) # => 2 'foo'.index('o', -2) # => 1 'foo'.index('o', -3) # => 1 'foo'.index('o', -4) # => nil 'foo'.index(/o./, -2) # => 1 'foo'.index(/.o/, -2) # => 1
Related: String#rindex
.
Returns the 0-based integer index of a substring of self
specified by object
(a string or Regexp
) and offset
, or nil
if there is no such substring; the returned index is the count of bytes (not characters).
When object
is a string, returns the index of the first found substring equal to object
:
s = 'foo' # => "foo" s.size # => 3 # Three 1-byte characters. s.bytesize # => 3 # Three bytes. s.byteindex('f') # => 0 s.byteindex('o') # => 1 s.byteindex('oo') # => 1 s.byteindex('ooo') # => nil
When object
is a Regexp
, returns the index of the first found substring matching object
; updates Regexp-related global variables:
s = 'foo' s.byteindex(/f/) # => 0 $~ # => #<MatchData "f"> s.byteindex(/o/) # => 1 s.byteindex(/oo/) # => 1 s.byteindex(/ooo/) # => nil $~ # => nil
Integer argument offset
, if given, specifies the 0-based index of the byte where searching is to begin.
When offset
is non-negative, searching begins at byte position offset
:
s = 'foo' s.byteindex('o', 1) # => 1 s.byteindex('o', 2) # => 2 s.byteindex('o', 3) # => nil
When offset
is negative, counts backward from the end of self
:
s = 'foo' s.byteindex('o', -1) # => 2 s.byteindex('o', -2) # => 1 s.byteindex('o', -3) # => 1 s.byteindex('o', -4) # => nil
Raises IndexError
if the byte at offset
is not the first byte of a character:
s = "\uFFFF\uFFFF" # => "\uFFFF\uFFFF" s.size # => 2 # Two 3-byte characters. s.bytesize # => 6 # Six bytes. s.byteindex("\uFFFF") # => 0 s.byteindex("\uFFFF", 1) # Raises IndexError s.byteindex("\uFFFF", 2) # Raises IndexError s.byteindex("\uFFFF", 3) # => 3 s.byteindex("\uFFFF", 4) # Raises IndexError s.byteindex("\uFFFF", 5) # Raises IndexError s.byteindex("\uFFFF", 6) # => nil
Related: see Querying.
Returns a printable version of self
, enclosed in double-quotes, and with special characters escaped:
s = "foo\tbar\tbaz\n" s.inspect # => "\"foo\\tbar\\tbaz\\n\""
Forms substrings (“lines”) of self
according to the given arguments (see String#each_line
for details); returns the lines in an array.
Returns an array of the codepoints in self
; each codepoint is the integer value for a character:
'hello'.codepoints # => [104, 101, 108, 108, 111] 'тест'.codepoints # => [1090, 1077, 1089, 1090] 'こんにちは'.codepoints # => [12371, 12435, 12395, 12385, 12399]
Returns the Symbol
corresponding to str, creating the symbol if it did not previously exist. See Symbol#id2name
.
"Koala".intern #=> :Koala s = 'cat'.to_sym #=> :cat s == :cat #=> true s = '@cat'.to_sym #=> :@cat s == :@cat #=> true
This can also be used to create symbols that cannot be represented using the :xxx
notation.
'cat and dog'.to_sym #=> :"cat and dog"
Returns true
if self
contains other_string
, false
otherwise:
s = 'foo' s.include?('f') # => true s.include?('fo') # => true s.include?('food') # => false
Returns a left-justified copy of self
.
If integer argument size
is greater than the size (in characters) of self
, returns a new string of length size
that is a copy of self
, left justified and padded on the right with pad_string
:
'hello'.ljust(10) # => "hello " ' hello'.ljust(10) # => " hello " 'hello'.ljust(10, 'ab') # => "helloababa" 'тест'.ljust(10) # => "тест " 'こんにちは'.ljust(10) # => "こんにちは "
If size
is not greater than the size of self
, returns a copy of self
:
'hello'.ljust(5) # => "hello" 'hello'.ljust(1) # => "hello"
Related: String#rjust
, String#center
.
Returns a right-justified copy of self
.
If integer argument size
is greater than the size (in characters) of self
, returns a new string of length size
that is a copy of self
, right justified and padded on the left with pad_string
:
'hello'.rjust(10) # => " hello" 'hello '.rjust(10) # => " hello " 'hello'.rjust(10, 'ab') # => "ababahello" 'тест'.rjust(10) # => " тест" 'こんにちは'.rjust(10) # => " こんにちは"
If size
is not greater than the size of self
, returns a copy of self
:
'hello'.rjust(5, 'ab') # => "hello" 'hello'.rjust(1, 'ab') # => "hello"
Related: String#ljust
, String#center
.
Returns a copy of self
with each character specified by string selector
translated to the corresponding character in string replacements
. The correspondence is positional:
Each occurrence of the first character specified by selector
is translated to the first character in replacements
.
Each occurrence of the second character specified by selector
is translated to the second character in replacements
.
And so on.
Example:
'hello'.tr('el', 'ip') #=> "hippo"
If replacements
is shorter than selector
, it is implicitly padded with its own last character:
'hello'.tr('aeiou', '-') # => "h-ll-" 'hello'.tr('aeiou', 'AA-') # => "hAll-"
Arguments selector
and replacements
must be valid character selectors (see Character Selectors), and may use any of its valid forms, including negation, ranges, and escaping:
# Negation. 'hello'.tr('^aeiou', '-') # => "-e--o" # Ranges. 'ibm'.tr('b-z', 'a-z') # => "hal" # Escapes. 'hel^lo'.tr('\^aeiou', '-') # => "h-l-l-" # Escaped leading caret. 'i-b-m'.tr('b\-z', 'a-z') # => "ibabm" # Escaped embedded hyphen. 'foo\\bar'.tr('ab\\', 'XYZ') # => "fooZYXr" # Escaped backslash.
Like String#tr
, but modifies self
in place. Returns self
if any changes were made, nil
otherwise.
Returns a string containing the IP address representation in canonical form.
Splits str
into an array of tokens in the same way the UNIX Bourne shell does.
See Shellwords.shellsplit
for details.
Escapes str
so that it can be safely used in a Bourne shell command line.
See Shellwords.shellescape
for details.
Extracts data from self
.
If block
is not given, forming objects that become the elements of a new array, and returns that array. Otherwise, yields each object.
See Packed Data.
Like String#unpack
, but unpacks and returns only the first extracted object. See Packed Data.
Returns a new String object containing the given string
.
The options
are optional keyword options (see below).
With no argument given and keyword encoding
also not given, returns an empty string with the Encoding
ASCII-8BIT
:
s = String.new # => "" s.encoding # => #<Encoding:ASCII-8BIT>
With argument string
given and keyword option encoding
not given, returns a new string with the same encoding as string
:
s0 = 'foo'.encode(Encoding::UTF_16) s1 = String.new(s0) s1.encoding # => #<Encoding:UTF-16 (dummy)>
(Unlike String.new, a string literal like ''
or a string literal always has script encoding.)
With keyword option encoding
given, returns a string with the specified encoding; the encoding
may be an Encoding
object, an encoding name, or an encoding name alias:
String.new(encoding: Encoding::US_ASCII).encoding # => #<Encoding:US-ASCII> String.new('', encoding: Encoding::US_ASCII).encoding # => #<Encoding:US-ASCII> String.new('foo', encoding: Encoding::US_ASCII).encoding # => #<Encoding:US-ASCII> String.new('foo', encoding: 'US-ASCII').encoding # => #<Encoding:US-ASCII> String.new('foo', encoding: 'ASCII').encoding # => #<Encoding:US-ASCII>
The given encoding need not be valid for the string’s content, and its validity is not checked:
s = String.new('こんにちは', encoding: 'ascii') s.valid_encoding? # => false
But the given encoding
itself is checked:
String.new('foo', encoding: 'bar') # Raises ArgumentError.
With keyword option capacity
given, the given value is advisory only, and may or may not set the size of the internal buffer, which may in turn affect performance:
String.new('foo', capacity: 1) # Buffer size is at least 4 (includes terminal null byte). String.new('foo', capacity: 4096) # Buffer size is at least 4; # may be equal to, greater than, or less than 4096.
Compares self
and other_string
, returning:
-1 if other_string
is larger.
0 if the two are equal.
1 if other_string
is smaller.
nil
if the two are incomparable.
Examples:
'foo' <=> 'foo' # => 0 'foo' <=> 'food' # => -1 'food' <=> 'foo' # => 1 'FOO' <=> 'foo' # => -1 'foo' <=> 'FOO' # => 1 'foo' <=> 1 # => nil
Related: see Comparing.
Returns whether object
is equal to self
.
When object
is a string, returns whether object
has the same length and content as self
:
s = 'foo' s == 'foo' # => true s == 'food' # => false s == 'FOO' # => false
Returns false
if the two strings’ encodings are not compatible:
"\u{e4 f6 fc}".encode(Encoding::ISO_8859_1) == ("\u{c4 d6 dc}") # => false
When object
is not a string:
If object
responds to method to_str
, object == self
is called and its return value is returned.
If object
does not respond to to_str
, false
is returned.
Related: Comparing.
Returns whether object
is equal to self
.
When object
is a string, returns whether object
has the same length and content as self
:
s = 'foo' s == 'foo' # => true s == 'food' # => false s == 'FOO' # => false
Returns false
if the two strings’ encodings are not compatible:
"\u{e4 f6 fc}".encode(Encoding::ISO_8859_1) == ("\u{c4 d6 dc}") # => false
When object
is not a string:
If object
responds to method to_str
, object == self
is called and its return value is returned.
If object
does not respond to to_str
, false
is returned.
Related: Comparing.