Looks ahead to see if the pattern
exists anywhere in the string, without advancing the scan pointer. This predicates whether a scan_until
will return a value.
s = StringScanner.new('test string') s.exist? /s/ # -> 3 s.scan /test/ # -> "test" s.exist? /s/ # -> 2 s.exist? /e/ # -> nil
Returns the subgroups in the most recent match (not including the full match). 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.captures # -> ["Fri", "Dec", "12"] s.scan(/(\w+) (\w+) (\d+) /) # -> nil s.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}"
Hash#to_s
is an alias for Hash#inspect
.
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))
Returns “ARGF”.
Returns an integer representing the numeric file descriptor for the current file. Raises an ArgumentError
if there isn’t a current file.
ARGF.fileno #=> 3
Reads length bytes from ARGF
. The files named on the command line are concatenated and treated as a single file by this method, so when called without arguments the contents of this pseudo file are returned in their entirety.
length must be a non-negative integer or nil
.
If length is a positive integer, read
tries to read length bytes without any conversion (binary mode). It returns nil
if an EOF is encountered before anything can be read. Fewer than length bytes are returned if an EOF is encountered during the read. In the case of an integer length, the resulting string is always in ASCII-8BIT encoding.
If length is omitted or is nil
, it reads until EOF and the encoding conversion is applied, if applicable. A string is returned even if EOF is encountered before any data is read.
If length is zero, it returns an empty string (""
).
If the optional outbuf argument is present, it must reference a String
, which will receive the data. The outbuf will contain only the received data after the method call even if it is not empty at the beginning.
For example:
$ echo "small" > small.txt $ echo "large" > large.txt $ ./glark.rb small.txt large.txt ARGF.read #=> "small\nlarge" ARGF.read(200) #=> "small\nlarge" ARGF.read(2) #=> "sm" ARGF.read(0) #=> ""
Note that this method behaves like the fread() function in C. This means it retries to invoke read(2) system calls to read data with the specified length. If you need the behavior like a single read(2) system call, consider ARGF#readpartial
or ARGF#read_nonblock
.
Reads at most maxlen bytes from the ARGF
stream.
If the optional outbuf argument is present, it must reference a String
, which will receive the data. The outbuf will contain only the received data after the method call even if it is not empty at the beginning.
It raises EOFError
on end of ARGF
stream. Since ARGF
stream is a concatenation of multiple files, internally EOF is occur for each file. ARGF.readpartial
returns empty strings for EOFs except the last one and raises EOFError
for the last one.
Reads each file in ARGF
in its entirety, returning an Array
containing lines from the files. Lines are assumed to be separated by sep.
lines = ARGF.readlines lines[0] #=> "This is line one\n"
Reads each file in ARGF
in its entirety, returning an Array
containing lines from the files. Lines are assumed to be separated by sep.
lines = ARGF.readlines lines[0] #=> "This is line one\n"
Returns the next line from the current file in ARGF
.
By default lines are assumed to be separated by $/
; to use a different character as a separator, supply it as a String
for the sep argument.
The optional limit argument specifies how many characters of each line to return. By default all characters are returned.
An EOFError
is raised at the end of the file.
Reads the next character from ARGF
and returns it as a String
. Raises an EOFError
after the last character of the last file has been read.
For example:
$ echo "foo" > file $ ruby argf.rb file ARGF.readchar #=> "f" ARGF.readchar #=> "o" ARGF.readchar #=> "o" ARGF.readchar #=> "\n" ARGF.readchar #=> end of file reached (EOFError)