Returns true if sym
starts with one of the prefixes
given. Each of the prefixes
should be a String
or a Regexp
.
:hello.start_with?("hell") #=> true :hello.start_with?(/H/i) #=> true # returns true if one of the prefixes matches. :hello.start_with?("heaven", "hell") #=> true :hello.start_with?("heaven", "paradise") #=> false
Returns true if sym
ends with one of the suffixes
given.
:hello.end_with?("ello") #=> true # returns true if one of the +suffixes+ matches. :hello.end_with?("heaven", "ello") #=> true :hello.end_with?("heaven", "paradise") #=> false
Returns true if this class can be used to create an instance from a serialised JSON
string. The class has to implement a class method json_create that expects a hash as first parameter. The hash should include the required data.
creates an Addrinfo
object from the arguments.
The arguments are interpreted as similar to self.
Addrinfo.tcp("0.0.0.0", 4649).family_addrinfo("www.ruby-lang.org", 80) #=> #<Addrinfo: 221.186.184.68:80 TCP (www.ruby-lang.org:80)> Addrinfo.unix("/tmp/sock").family_addrinfo("/tmp/sock2") #=> #<Addrinfo: /tmp/sock2 SOCK_STREAM>
creates a new Socket
connected to the address of local_addrinfo
.
If local_addrinfo is nil, the address of the socket is not bound.
The timeout specify the seconds for timeout. Errno::ETIMEDOUT is raised when timeout occur.
If a block is given the created socket is yielded for each address.
Returns the IP address and port number as 2-element array.
Addrinfo.tcp("127.0.0.1", 80).ip_unpack #=> ["127.0.0.1", 80] Addrinfo.tcp("::1", 80).ip_unpack #=> ["::1", 80]
Returns true for IPv4 private address (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16). It returns false otherwise.
Returns the Encoding
object that represents the encoding of the file. If the stream is write mode and no encoding is specified, returns nil
.
Returns the Encoding
of the internal string if conversion is specified. Otherwise returns nil
.
Returns the size of the most recent match in bytes, or nil
if there was no recent match. This is different than matched.size
, which will return the size in characters.
s = StringScanner.new('test string') s.check /\w+/ # -> "test" s.matched_size # -> 4 s.check /\d+/ # -> nil s.matched_size # -> nil
Returns the pre-match
(in the regular expression sense) of the last scan.s = StringScanner.new('test string') s.scan(/\w+/) # -> "test" s.scan(/\s+/) # -> " " s.pre_match # -> "test" s.post_match # -> "string"
Returns the post-match
(in the regular expression sense) of the last scan.s = StringScanner.new('test string') s.scan(/\w+/) # -> "test" s.scan(/\s+/) # -> " " s.pre_match # -> "test" s.post_match # -> "string"
Returns the subgroups in the most recent match at the given indices. If nothing was priorly matched, it returns nil.
s = StringScanner.new("Fri Dec 12 1975 14:39") s.scan(/(\w+) (\w+) (\d+) /) # -> "Fri Dec 12 " s.values_at 0, -1, 5, 2 # -> ["Fri Dec 12 ", "12", nil, "Dec"] s.scan(/(\w+) (\w+) (\d+) /) # -> nil s.values_at 0, -1, 5, 2 # -> nil
Creates GUID.
WIN32OLE.create_guid # => {1CB530F1-F6B1-404D-BCE6-1959BF91F4A8}
Translates and dispatches Windows message.
Hash#each
is an alias for Hash#each_pair
.
Calls the given block with each key-value pair; returns self
:
h = {foo: 0, bar: 1, baz: 2} h.each_pair {|key, value| puts "#{key}: #{value}"} # => {:foo=>0, :bar=>1, :baz=>2}
Output:
foo: 0 bar: 1 baz: 2
Returns a new Enumerator if no block given:
h = {foo: 0, bar: 1, baz: 2} e = h.each_pair # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:each_pair> h1 = e.each {|key, value| puts "#{key}: #{value}"} h1 # => {:foo=>0, :bar=>1, :baz=>2}
Output:
foo: 0 bar: 1 baz: 2
Returns a new Array containing values for the given keys
:
h = {foo: 0, bar: 1, baz: 2} h.values_at(:baz, :foo) # => [2, 0]
The default values are returned for any keys that are not found:
h.values_at(:hello, :foo) # => [nil, 0]
Yields each environment variable name and its value as a 2-element Array:
h = {} ENV.each_pair { |name, value| h[name] = value } # => ENV h # => {"bar"=>"1", "foo"=>"0"}
Returns an Enumerator
if no block given:
h = {} e = ENV.each_pair # => #<Enumerator: {"bar"=>"1", "foo"=>"0"}:each_pair> e.each { |name, value| h[name] = value } # => ENV h # => {"bar"=>"1", "foo"=>"0"}
Returns an Array
containing the environment variable values associated with the given names:
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.values_at('foo', 'baz') # => ["0", "2"]
Returns nil
in the Array
for each name that is not an ENV
name:
ENV.values_at('foo', 'bat', 'bar', 'bam') # => ["0", nil, "1", nil]
Returns an empty Array if no names given.
Raises an exception if any name is invalid. See Invalid Names and Values.
Returns the external encoding for files read from ARGF
as an Encoding
object. The external encoding is the encoding of the text as stored in a file. Contrast with ARGF.internal_encoding
, which is the encoding used to represent this text within Ruby.
To set the external encoding use ARGF.set_encoding
.
For example:
ARGF.external_encoding #=> #<Encoding:UTF-8>
Returns the internal encoding for strings read from ARGF
as an Encoding
object.
If ARGF.set_encoding
has been called with two encoding names, the second is returned. Otherwise, if Encoding.default_external
has been set, that value is returned. Failing that, if a default external encoding was specified on the command-line, that value is used. If the encoding is unknown, nil
is returned.
Returns the String created by generating CSV from ary
using the specified options
.
Argument ary
must be an Array.
Special options:
Option :row_sep
defaults to "\n"> on Ruby 3.0 or later and <tt>$INPUT_RECORD_SEPARATOR
($/
) otherwise.:
$INPUT_RECORD_SEPARATOR # => "\n"
This method accepts an additional option, :encoding
, which sets the base Encoding
for the output. This method will try to guess your Encoding
from the first non-nil
field in row
, if possible, but you may need to use this parameter as a backup plan.
For other options
, see Options for Generating.
Returns the String generated from an Array:
CSV.generate_line(['foo', '0']) # => "foo,0\n"
Raises an exception if ary
is not an Array:
# Raises NoMethodError (undefined method `find' for :foo:Symbol) CSV.generate_line(:foo)
Returns the String created by generating CSV from using the specified options
.
Argument rows
must be an Array of row. Row
is Array of String or CSV::Row.
Special options:
Option :row_sep
defaults to "\n"
on Ruby 3.0 or later and $INPUT_RECORD_SEPARATOR
($/
) otherwise.:
$INPUT_RECORD_SEPARATOR # => "\n"
This method accepts an additional option, :encoding
, which sets the base Encoding
for the output. This method will try to guess your Encoding
from the first non-nil
field in row
, if possible, but you may need to use this parameter as a backup plan.
For other options
, see Options for Generating.
Returns the String generated from an
CSV.generate_lines(['foo', '0'], ['bar', '1'], ['baz', '2']) # => "foo,0\nbar,1\nbaz.2\n"
Raises an exception
# Raises NoMethodError (undefined method `find' for :foo:Symbol) CSV.generate_lines(:foo)