Assigns the underlying string as other_string
, and sets position to zero; returns other_string
:
StringIO.open('foo') do |strio| p strio.string strio.string = 'bar' p strio.string end
Output:
"foo" "bar"
Related: StringIO#string
(returns the underlying string).
Sets the current position and line number to zero; see Position and Line Number.
See IO#read
.
See IO#pread
.
Sets both [byte position] and [character position] to zero, and clears [match values]; returns self
:
scanner = StringScanner.new('foobarbaz') scanner.exist?(/bar/) # => 6 scanner.reset # => #<StringScanner 0/9 @ "fooba..."> put_situation(scanner) # Situation: # pos: 0 # charpos: 0 # rest: "foobarbaz" # rest_size: 9 # => nil match_values_cleared?(scanner) # => true
Returns the [stored string]:
scanner = StringScanner.new('foobar') scanner.string # => "foobar" scanner.concat('baz') scanner.string # => "foobarbaz"
Replaces the [stored string] with the given other_string
:
Sets both [positions] to zero.
Clears [match values].
Returns other_string
.
scanner = StringScanner.new('foobar') scanner.scan(/foo/) put_situation(scanner) # Situation: # pos: 3 # charpos: 3 # rest: "bar" # rest_size: 3 match_values_cleared?(scanner) # => false scanner.string = 'baz' # => "baz" put_situation(scanner) # Situation: # pos: 0 # charpos: 0 # rest: "baz" # rest_size: 3 match_values_cleared?(scanner) # => true
Attempts to [match] the given pattern
anywhere (at any [position]) n the [target substring]; does not modify the [positions].
If the match succeeds:
Returns a byte offset: the distance in bytes between the current [position] and the end of the matched substring.
Sets all [match values].
scanner = StringScanner.new('foobarbazbatbam') scanner.pos = 6 scanner.exist?(/bat/) # => 6 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:
Returns nil
.
Clears all [match values].
scanner.exist?(/nope/) # => nil match_values_cleared?(scanner) # => true
Returns the array of [captured match values] at indexes (1..)
if the most recent match attempt succeeded, or nil
otherwise:
scanner = StringScanner.new('Fri Dec 12 1975 14:39') scanner.captures # => nil scanner.exist?(/(?<wday>\w+) (?<month>\w+) (?<day>\d+) /) scanner.captures # => ["Fri", "Dec", "12"] scanner.values_at(*0..4) # => ["Fri Dec 12 ", "Fri", "Dec", "12", nil] scanner.exist?(/Fri/) scanner.captures # => [] scanner.scan(/nope/) scanner.captures # => nil
Rebuilds the hash table by recomputing the hash index for each key; returns self
.
The hash table becomes invalid if the hash value of a key has changed after the entry was created. See Modifying an Active Hash Key.
For an instance of Hash
, returns self
.
For a subclass of Hash
, returns a new Hash
containing the content of self
.
When a block is given, returns a new Hash
object whose content is based on the block; the block should return a 2-element Array
object specifying the key-value pair to be included in the returned Array:
h = {foo: 0, bar: 1, baz: 2} h1 = h.to_h {|key, value| [value, key] } h1 # => {0=>:foo, 1=>:bar, 2=>:baz}
Returns a new Array
of 2-element Array
objects; each nested Array
contains a key-value pair from self
:
h = {foo: 0, bar: 1, baz: 2} h.to_a # => [[:foo, 0], [:bar, 1], [:baz, 2]]
Returns a new Hash
object whose entries are all those from self
for which the block returns false
or nil
:
h = {foo: 0, bar: 1, baz: 2} h1 = h.reject {|key, value| key.start_with?('b') } h1 # => {foo: 0}
Returns a new Enumerator
if no block given:
h = {foo: 0, bar: 1, baz: 2} e = h.reject # => #<Enumerator: {foo: 0, bar: 1, baz: 2}:reject> h1 = e.each {|key, value| key.start_with?('b') } h1 # => {foo: 0}
Returns self
, whose remaining entries are those for which the block returns false
or nil
:
h = {foo: 0, bar: 1, baz: 2} h.reject! {|key, value| value < 2 } # => {baz: 2}
Returns nil
if no entries are removed.
Returns a new Enumerator
if no block given:
h = {foo: 0, bar: 1, baz: 2} e = h.reject! # => #<Enumerator: {foo: 0, bar: 1, baz: 2}:reject!> e.each {|key, value| key.start_with?('b') } # => {foo: 0}
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}
Returns a new String
containing the hash entries:
h = {foo: 0, bar: 1, baz: 2} h.inspect # => "{foo: 0, bar: 1, baz: 2}"
Yields each environment variable name and its value as a 2-element Array
. Returns a Hash
whose items are determined by the block. When the block returns a truthy value, the name/value pair is added to the return Hash
; otherwise the pair is ignored:
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.reject { |name, value| name.start_with?('b') } # => {"foo"=>"0"}
Returns an Enumerator
if no block given:
e = ENV.reject e.each { |name, value| name.start_with?('b') } # => {"foo"=>"0"}
Similar to ENV.delete_if
, but returns nil
if no changes were made.
Yields each environment variable name and its value as a 2-element Array
, deleting each environment variable for which the block returns a truthy value, and returning ENV
(if any deletions) or nil
(if not):
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.reject! { |name, value| name.start_with?('b') } # => ENV ENV # => {"foo"=>"0"} ENV.reject! { |name, value| name.start_with?('b') } # => nil
Returns an Enumerator
if no block given:
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') e = ENV.reject! # => #<Enumerator: {"bar"=>"1", "baz"=>"2", "foo"=>"0"}:reject!> e.each { |name, value| name.start_with?('b') } # => ENV ENV # => {"foo"=>"0"} e.each { |name, value| name.start_with?('b') } # => nil
Replaces the entire content of the environment variables with the name/value pairs in the given hash
; returns ENV
.
Replaces the content of ENV
with the given pairs:
ENV.replace('foo' => '0', 'bar' => '1') # => ENV ENV.to_hash # => {"bar"=>"1", "foo"=>"0"}
Raises an exception if a name or value is invalid (see Invalid Names and Values):
ENV.replace('foo' => '0', :bar => '1') # Raises TypeError (no implicit conversion of Symbol into String) ENV.replace('foo' => '0', 'bar' => 1) # Raises TypeError (no implicit conversion of Integer into String) ENV.to_hash # => {"bar"=>"1", "foo"=>"0"}
Returns the contents of ENV
as an Array
of 2-element Arrays, each of which is a name/value pair:
ENV.replace('foo' => '0', 'bar' => '1') ENV.to_a # => [["bar", "1"], ["foo", "0"]]
With no block, returns a Hash
containing all name/value pairs from ENV:
ENV.replace('foo' => '0', 'bar' => '1') ENV.to_h # => {"bar"=>"1", "foo"=>"0"}
With a block, returns a Hash
whose items are determined by the block. Each name/value pair in ENV
is yielded to the block. The block must return a 2-element Array
(name/value pair) that is added to the return Hash
as a key and value:
ENV.to_h { |name, value| [name.to_sym, value.to_i] } # => {bar: 1, foo: 0}
Raises an exception if the block does not return an Array:
ENV.to_h { |name, value| name } # Raises TypeError (wrong element type String (expected array))
Raises an exception if the block returns an Array
of the wrong size:
ENV.to_h { |name, value| [name] } # Raises ArgumentError (element has wrong array length (expected 2, was 1))