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>
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 the IP address as a string.
Addrinfo.tcp("127.0.0.1", 80).ip_address #=> "127.0.0.1" Addrinfo.tcp("::1", 80).ip_address #=> "::1"
Returns the port number as an integer.
Addrinfo.tcp("127.0.0.1", 80).ip_port #=> 80 Addrinfo.tcp("::1", 80).ip_port #=> 80
Returns true for IPv4 loopback address (127.0.0.0/8). It returns false otherwise.
Returns true for IPv6 unspecified address (::). It returns false otherwise.
Returns true for IPv6 loopback address (::1). It returns false otherwise.
Returns true for IPv6 link local address (fe80::/10). It returns false otherwise.
Returns true for IPv6 site local address (fec0::/10). It returns false otherwise.
Returns true for IPv4-mapped IPv6 address (::ffff:0:0/80). It returns false otherwise.
Returns true for IPv4-compatible IPv6 address (::/80). It returns false otherwise.
Closes self
for writing; closed-read setting remains unchanged.
Raises IOError
if writing is attempted.
Related: StringIO#close
, StringIO#close_read
.
Returns true
if self
is closed for writing, false
otherwise.
Returns the substring that follows the matched substring from the most recent match attempt if it was successful, or nil
otherwise; see [Basic Match Values]:
scanner = StringScanner.new('foobarbaz') scanner.post_match # => nil scanner.pos = 3 scanner.match?(/bar/) # => 3 scanner.post_match # => "baz" scanner.match?(/nope/) # => nil scanner.post_match # => nil
Returns the size (in bytes) of the rest
of the [stored string]:
scanner = StringScanner.new('foobarbaz') scanner.rest # => "foobarbaz" scanner.rest_size # => 9 scanner.pos = 3 scanner.rest # => "barbaz" scanner.rest_size # => 6 scanner.terminate scanner.rest # => "" scanner.rest_size # => 0
Evaluates a string containing Ruby
source code, or the given block, within the context of the receiver (obj). In order to set the context, the variable self
is set to obj while the code is executing, giving the code access to obj’s instance variables and private methods.
When instance_eval
is given a block, obj is also passed in as the block’s only argument.
When instance_eval
is given a String
, the optional second and third parameters supply a filename and starting line number that are used when reporting compilation errors.
class KlassWithSecret def initialize @secret = 99 end private def the_secret "Ssssh! The secret is #{@secret}." end end k = KlassWithSecret.new k.instance_eval { @secret } #=> 99 k.instance_eval { the_secret } #=> "Ssssh! The secret is 99." k.instance_eval {|obj| obj == self } #=> true
Executes the given block within the context of the receiver (obj). In order to set the context, the variable self
is set to obj while the code is executing, giving the code access to obj’s instance variables. Arguments are passed as block parameters.
class KlassWithSecret def initialize @secret = 99 end end k = KlassWithSecret.new k.instance_exec(5) {|x| @secret+x } #=> 104
If object
is a hash, returns object
.
Otherwise if object
responds to :to_hash
, calls object.to_hash
; returns the result if it is a hash, or raises TypeError
if not.
Otherwise if object
does not respond to :to_hash
, returns nil
.
With an argument, a block, or both given, derives a new hash new_hash
from self
, the argument, and/or the block; all, some, or none of its keys may be different from those in self
.
With a block given and no argument, new_hash
has keys determined only by the block.
For each key/value pair old_key/value
in self
, calls the block with old_key
; the block’s return value becomes new_key
; sets new_hash[new_key] = value
; a duplicate key overwrites:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys {|old_key| old_key.to_s } # => {"foo" => 0, "bar" => 1, "baz" => 2} h.transform_keys {|old_key| 'xxx' } # => {"xxx" => 2}
With argument other_hash
given and no block, new_hash
may have new keys provided by other_hash
and unchanged keys provided by self
.
For each key/value pair old_key/old_value
in self
, looks for key old_key
in other_hash
:
If old_key
is found, its value other_hash[old_key]
is taken as new_key
; sets new_hash[new_key] = value
; a duplicate key overwrites:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys(baz: :BAZ, bar: :BAR, foo: :FOO) # => {FOO: 0, BAR: 1, BAZ: 2} h.transform_keys(baz: :FOO, bar: :FOO, foo: :FOO) # => {FOO: 2}
If old_key
is not found, sets new_hash[old_key] = value
; a duplicate key overwrites:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys({}) # => {foo: 0, bar: 1, baz: 2} h.transform_keys(baz: :foo) # => {foo: 2, bar: 1}
Unused keys in other_hash
are ignored:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys(bat: 3) # => {foo: 0, bar: 1, baz: 2}
With both argument other_hash
and a block given, new_hash
has new keys specified by other_hash
or by the block, and unchanged keys provided by self
.
For each pair old_key
and value
in self
:
If other_hash
has key old_key
(with value new_key
), does not call the block for that key; sets new_hash[new_key] = value
; a duplicate key overwrites:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys(baz: :BAZ, bar: :BAR, foo: :FOO) {|key| fail 'Not called' } # => {FOO: 0, BAR: 1, BAZ: 2}
If other_hash
does not have key old_key
, calls the block with old_key
and takes its return value as new_key
; sets new_hash[new_key] = value
; a duplicate key overwrites:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys(baz: :BAZ) {|key| key.to_s.reverse } # => {"oof" => 0, "rab" => 1, BAZ: 2} h.transform_keys(baz: :BAZ) {|key| 'ook' } # => {"ook" => 1, BAZ: 2}
With no argument and no block given, returns a new Enumerator
.
Related: see Methods for Transforming Keys and Values.
With an argument, a block, or both given, derives keys from the argument, the block, and self
; all, some, or none of the keys in self
may be changed.
With a block given and no argument, derives keys only from the block; all, some, or none of the keys in self
may be changed.
For each key/value pair old_key/value
in self
, calls the block with old_key
; the block’s return value becomes new_key
; removes the entry for old_key
: self.delete(old_key)
; sets self[new_key] = value
; a duplicate key overwrites:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys! {|old_key| old_key.to_s } # => {"foo" => 0, "bar" => 1, "baz" => 2} h = {foo: 0, bar: 1, baz: 2} h.transform_keys! {|old_key| 'xxx' } # => {"xxx" => 2}
With argument other_hash
given and no block, derives keys for self
from other_hash
and self
; all, some, or none of the keys in self
may be changed.
For each key/value pair old_key/old_value
in self
, looks for key old_key
in other_hash
:
If old_key
is found, takes value other_hash[old_key]
as new_key
; removes the entry for old_key
: self.delete(old_key)
; sets self[new_key] = value
; a duplicate key overwrites:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys!(baz: :BAZ, bar: :BAR, foo: :FOO) # => {FOO: 0, BAR: 1, BAZ: 2} h = {foo: 0, bar: 1, baz: 2} h.transform_keys!(baz: :FOO, bar: :FOO, foo: :FOO) # => {FOO: 2}
If old_key
is not found, does nothing:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys!({}) # => {foo: 0, bar: 1, baz: 2} h.transform_keys!(baz: :foo) # => {foo: 2, bar: 1}
Unused keys in other_hash
are ignored:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys!(bat: 3) # => {foo: 0, bar: 1, baz: 2}
With both argument other_hash
and a block given, derives keys from other_hash
, the block, and self
; all, some, or none of the keys in self
may be changed.
For each pair old_key
and value
in self
:
If other_hash
has key old_key
(with value new_key
), does not call the block for that key; removes the entry for old_key
: self.delete(old_key)
; sets self[new_key] = value
; a duplicate key overwrites:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys!(baz: :BAZ, bar: :BAR, foo: :FOO) {|key| fail 'Not called' } # => {FOO: 0, BAR: 1, BAZ: 2}
If other_hash
does not have key old_key
, calls the block with old_key
and takes its return value as new_key
; removes the entry for old_key
: self.delete(old_key)
; sets self[new_key] = value
; a duplicate key overwrites:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys!(baz: :BAZ) {|key| key.to_s.reverse } # => {"oof" => 0, "rab" => 1, BAZ: 2} h = {foo: 0, bar: 1, baz: 2} h.transform_keys!(baz: :BAZ) {|key| 'ook' } # => {"ook" => 1, BAZ: 2}
With no argument and no block given, returns a new Enumerator
.
Related: see Methods for Transforming Keys and Values.
With a block given, returns a new hash new_hash
; for each pair key
/value
in self
, calls the block with value
and captures its return as new_value
; adds to new_hash
the entry key
/new_value
:
h = {foo: 0, bar: 1, baz: 2} h1 = h.transform_values {|value| value * 100} h1 # => {foo: 0, bar: 100, baz: 200}
With no block given, returns a new Enumerator
.
Related: see Methods for Transforming Keys and Values.
With a block given, changes the values of self
as determined by the block; returns self
.
For each entry key
/old_value
in self
, calls the block with old_value
, captures its return value as new_value
, and sets self[key] = new_value
:
h = {foo: 0, bar: 1, baz: 2} h.transform_values! {|value| value * 100} # => {foo: 0, bar: 100, baz: 200}
With no block given, returns a new Enumerator
.
Related: see Methods for Transforming Keys and Values.
Returns true if the ipaddr is an IPv4-mapped IPv6 address.