returns a string which shows the sockaddr in addrinfo with human-readable form.
Addrinfo.tcp("localhost", 80).inspect_sockaddr #=> "127.0.0.1:80" Addrinfo.tcp("ip6-localhost", 80).inspect_sockaddr #=> "[::1]:80" Addrinfo.unix("/tmp/sock").inspect_sockaddr #=> "/tmp/sock"
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 true for IPv4 multicast address (224.0.0.0/4). It returns false otherwise.
Returns true for IPv6 multicast address (ff00::/8). It returns false otherwise.
Returns true for IPv6 link local address (fe80::/10). 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.
Calls the block with each remaining line read from the stream; does nothing if already at end-of-file; returns self
. See Line IO.
With a block given, calls the block with each remaining codepoint in the stream; see Codepoint IO.
With no block given, returns an enumerator.
Returns a shallow copy of self
; the [stored string] in the copy is the same string as in self
.
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
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
.
Replaces the entire contents of self
with the contents of other_hash
; returns self
:
h = {foo: 0, bar: 1, baz: 2} h.replace({bat: 3, bam: 4}) # => {bat: 3, bam: 4}
Related: see Methods for Assigning.
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 an enumerator which iterates over each line (separated by sep, which defaults to your platform’s newline character) of each file in ARGV
. If a block is supplied, each line in turn will be yielded to the block, otherwise an enumerator is returned. The optional limit argument is an Integer
specifying the maximum length of each line; longer lines will be split according to this limit.
This method allows you to treat the files supplied on the command line as a single file consisting of the concatenation of each named file. After the last line of the first file has been returned, the first line of the second file is returned. The ARGF.filename
and ARGF.lineno
methods can be used to determine the filename of the current line and line number of the whole input, respectively.
For example, the following code prints out each line of each named file prefixed with its line number, displaying the filename once per file:
ARGF.each_line do |line| puts ARGF.filename if ARGF.file.lineno == 1 puts "#{ARGF.file.lineno}: #{line}" end
While the following code prints only the first file’s name at first, and the contents with line number counted through all named files.
ARGF.each_line do |line| puts ARGF.filename if ARGF.lineno == 1 puts "#{ARGF.lineno}: #{line}" end
Iterates over each codepoint of each file in ARGF
.
This method allows you to treat the files supplied on the command line as a single file consisting of the concatenation of each named file. After the last codepoint of the first file has been returned, the first codepoint of the second file is returned. The ARGF.filename
method can be used to determine the name of the file in which the current codepoint appears.
If no block is given, an enumerator is returned instead.
Returns the file extension appended to the names of backup copies of modified files under in-place edit mode. This value can be set using ARGF.inplace_mode=
or passing the -i
switch to the Ruby
binary.
Sets the filename extension for in-place editing mode to the given String
. The backup copy of each file being edited has this value appended to its filename.
For example:
$ ruby argf.rb file.txt ARGF.inplace_mode = '.bak' ARGF.each_line do |line| print line.sub("foo","bar") end
First, file.txt.bak is created as a backup copy of file.txt. Then, each line of file.txt has the first occurrence of “foo” replaced with “bar”.
Returns true if the ipaddr is a link-local address. IPv4 addresses in 169.254.0.0/16 reserved by RFC 3927 and link-local IPv6 Unicast Addresses in fe80::/10 reserved by RFC 4291 are considered link-local. Link-local IPv4 addresses in the IPv4-mapped IPv6 address range are also considered link-local.
Returns a string for DNS reverse lookup compatible with RFC1886.