See as_json
.
Calls the given block with each member name/value pair; returns self
:
Customer = Struct.new(:name, :address, :zip) # => Customer joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) joe.each_pair {|(name, value)| p "#{name} => #{value}" }
Output:
"name => Joe Smith" "address => 123 Maple, Anytown NC" "zip => 12345"
Returns an Enumerator
if no block is given.
Related: each
.
Returns an array of values from self
.
With integer arguments integers
given, returns an array containing each value given by one of integers
:
Customer = Struct.new(:name, :address, :zip) joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) joe.values_at(0, 2) # => ["Joe Smith", 12345] joe.values_at(2, 0) # => [12345, "Joe Smith"] joe.values_at(2, 1, 0) # => [12345, "123 Maple, Anytown NC", "Joe Smith"] joe.values_at(0, -3) # => ["Joe Smith", "Joe Smith"]
Raises IndexError
if any of integers
is out of range; see Array Indexes at Array
.
With integer range argument integer_range
given, returns an array containing each value given by the elements of the range; fills with nil
values for range elements larger than the structure:
joe.values_at(0..2) # => ["Joe Smith", "123 Maple, Anytown NC", 12345] joe.values_at(-3..-1) # => ["Joe Smith", "123 Maple, Anytown NC", 12345] joe.values_at(1..4) # => ["123 Maple, Anytown NC", 12345, nil, nil]
Raises RangeError
if any element of the range is negative and out of range; see Array Indexes at Array
.
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.
Iterates over each component of the path.
Pathname.new("/usr/bin/ruby").each_filename {|filename| ... } # yields "usr", "bin", and "ruby".
Returns an Enumerator
if no block was given.
enum = Pathname.new("/usr/bin/ruby").each_filename # ... do stuff ... enum.each { |e| ... } # yields "usr", "bin", and "ruby".
Return the path as a String
.
to_path
is implemented so Pathname
objects are usable with File.open
, etc.
Iterates over each line in the file and yields a String
object for each.
Iterates over the entries (files and subdirectories) in the directory, yielding a Pathname
object for each entry.
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-mapped IPv6 address (::ffff:0:0/80). It returns false otherwise.
Returns true for IPv4-compatible IPv6 address (::/80). It returns false otherwise.
Returns the socket path as a string.
Addrinfo.unix("/tmp/sock").unix_path #=> "/tmp/sock"
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 byte in the stream; see Byte IO.
With no block given, returns an enumerator.
With a block given, calls the block with each remaining codepoint in the stream; see Codepoint IO.
With no block given, returns an enumerator.
Attempts to [match] the given pattern
anywhere (at any [position]) in the [target substring]; does not modify the [positions].
If the match succeeds:
Sets all [match values].
Returns the matched substring, which extends from the current [position] to the end of the matched substring.
scanner = StringScanner.new('foobarbazbatbam') scanner.pos = 6 scanner.check_until(/bat/) # => "bazbat" put_match_values(scanner) # Basic match values: # matched?: true # matched_size: 3 # pre_match: "foobarbaz" # matched : "bat" # post_match: "bam" # Captured match values: # size: 1 # captures: [] # named_captures: {} # values_at: ["bat", nil] # []: # [0]: "bat" # [1]: nil put_situation(scanner) # Situation: # pos: 6 # charpos: 6 # rest: "bazbatbam" # rest_size: 9
If the match fails:
Clears all [match values].
Returns nil
.
scanner.check_until(/nope/) # => nil match_values_cleared?(scanner) # => true
Returns an array of captured substrings, or nil
of none.
For each specifier
, the returned substring is [specifier]
; see []
.
scanner = StringScanner.new('Fri Dec 12 1975 14:39') pattern = /(?<wday>\w+) (?<month>\w+) (?<day>\d+) / scanner.match?(pattern) scanner.values_at(*0..3) # => ["Fri Dec 12 ", "Fri", "Dec", "12"] scanner.values_at(*%i[wday month day]) # => ["Fri", "Dec", "12"]