Returns the value associated with the given key
if found.
If key
is not found, returns nil
.
Returns the value associated with the given key
if found.
If key
is not found, returns nil
.
Returns the function mapped to name
, that was created by either Fiddle::Importer.extern
or Fiddle::Importer.bind
Get the value for the parameter with a given key.
If the parameter has multiple values, only the first will be retrieved; use params
to get the array of values.
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.
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 that is a copy of string
.
With no arguments, returns the empty string with the Encoding
ASCII-8BIT
:
s = String.new s # => "" s.encoding # => #<Encoding:ASCII-8BIT>
With optional argument string
and no keyword arguments, returns a copy of string
with the same encoding:
String.new('foo') # => "foo" String.new('тест') # => "тест" String.new('こんにちは') # => "こんにちは"
(Unlike String.new, a string literal like ''
or a string literal always has script encoding.)
With optional keyword argument encoding
, returns a copy of string
with the specified encoding; the encoding
may be an Encoding
object, an encoding name, or an encoding name alias:
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 that 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 optional keyword argument capacity
, returns a copy of string
(or an empty string, if string
is not given); the given capacity
is advisory only, and may or may not set the size of the internal buffer, which may in turn affect performance:
String.new(capacity: 1) String.new('foo', capacity: 4096)
Note that Ruby strings are null-terminated internally, so the internal buffer size will be one or more bytes larger than the requested capacity depending on the encoding.
The string
, encoding
, and capacity
arguments may all be used together:
String.new('hello', encoding: 'UTF-8', capacity: 25)
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
Returns true
if object
has the same length and content; as self
; false
otherwise:
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("ISO-8859-1") == ("\u{c4 d6 dc}") # => false
If object
is not an instance of String
but responds to to_str
, then the two strings are compared using object.==
.
Returns true
if object
has the same length and content; as self
; false
otherwise:
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("ISO-8859-1") == ("\u{c4 d6 dc}") # => false
If object
is not an instance of String
but responds to to_str
, then the two strings are compared using object.==
.
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 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