Returns the character position of the scan pointer. In the ‘reset’ position, this value is zero. In the ‘terminated’ position (i.e. the string is exhausted), this value is the size of the string.
In short, it’s a 0-based index into the string.
s = StringScanner.new("abcädeföghi") s.charpos # -> 0 s.scan_until(/ä/) # -> "abcä" s.pos # -> 5 s.charpos # -> 4
Tests whether the given pattern
is matched from the current scan pointer. Returns the length of the match, or nil
. The scan pointer is not advanced.
s = StringScanner.new('test string') p s.match?(/\w+/) # -> 4 p s.match?(/\w+/) # -> 4 p s.match?(/\s+/) # -> nil
This returns the value that scan
would return, without advancing the scan pointer. The match register is affected, though.
s = StringScanner.new("Fri Dec 12 1975 14:39") s.check /Fri/ # -> "Fri" s.pos # -> 0 s.matched # -> "Fri" s.check /12/ # -> nil s.matched # -> nil
Mnemonic: it “checks” to see whether a scan
will return a value.
Scans one character and returns it. This method is multibyte character sensitive.
s = StringScanner.new("ab") s.getch # => "a" s.getch # => "b" s.getch # => nil $KCODE = 'EUC' s = StringScanner.new("\244\242") s.getch # => "\244\242" # Japanese hira-kana "A" in EUC-JP s.getch # => nil
Returns true
iff the last match was successful.
s = StringScanner.new('test string') s.match?(/\w+/) # => 4 s.matched? # => true s.match?(/\d+/) # => nil s.matched? # => false
Returns the last matched string.
s = StringScanner.new('test string') s.match?(/\w+/) # -> 4 s.matched # -> "test"
Iterates over each item of OLE collection which has IEnumVARIANT interface.
excel = WIN32OLE.new('Excel.Application') book = excel.workbooks.add sheets = book.worksheets(1) cells = sheets.cells("A1:A5") cells.each do |cell| cell.value = 10 end
Returns a value from the hash for the given key. If the key can’t be found, there are several options: With no other arguments, it will raise an KeyError
exception; if default is given, then that will be returned; if the optional code block is specified, then that will be run and its result returned.
h = { "a" => 100, "b" => 200 } h.fetch("a") #=> 100 h.fetch("z", "go fish") #=> "go fish" h.fetch("z") { |el| "go fish, #{el}"} #=> "go fish, z"
The following example shows that an exception is raised if the key is not found and a default value is not supplied.
h = { "a" => 100, "b" => 200 } h.fetch("z")
produces:
prog.rb:2:in `fetch': key not found (KeyError) from prog.rb:2
Calls block once for each key in hsh, passing the key-value pair as parameters.
If no block is given, an enumerator is returned instead.
h = { "a" => 100, "b" => 200 } h.each {|key, value| puts "#{key} is #{value}" }
produces:
a is 100 b is 200
Retrieves the environment variable name
.
If the given name does not exist and neither default
nor a block a provided an IndexError
is raised. If a block is given it is called with the missing name to provide a value. If a default value is given it will be returned when no block is given.
Yields each environment variable name
and value
.
If no block is given an Enumerator
is returned.
This method is intended as the primary interface for reading CSV
files. You pass a path
and any options
you wish to set for the read. Each row of file will be passed to the provided block
in turn.
The options
parameter can be anything CSV::new()
understands. This method also understands an additional :encoding
parameter that you can use to specify the Encoding
of the data in the file to be read. You must provide this unless your data is in Encoding::default_external()
. CSV
will use this to determine how to parse the data. You may provide a second Encoding
to have the data transcoded as it is read. For example, encoding: "UTF-32BE:UTF-8"
would read UTF-32BE data from the file but transcode it to UTF-8 before CSV
parses it.
Yields each row of the data source in turn.
Support for Enumerable
.
The data source must be open for reading.
Iterator version of ‘get’.
The block is called repeatedly with two arguments: The first is the option name. The second is the argument which followed it (if any). Example: (‘–opt’, ‘value’)
The option name is always converted to the first (preferred) name given in the original options to GetoptLong.new
.
Yields all elements of the matrix, starting with those of the first row, or returns an Enumerator
if no block given. Elements can be restricted by passing an argument:
:all (default): yields all elements
:diagonal: yields only elements on the diagonal
:off_diagonal: yields all elements except on the diagonal
:lower: yields only elements on or below the diagonal
:strict_lower: yields only elements below the diagonal
:strict_upper: yields only elements above the diagonal
:upper: yields only elements on or above the diagonal
Matrix[ [1,2], [3,4] ].each { |e| puts e }
# => prints the numbers 1 to 4
Matrix[ [1,2], [3,4] ].each(:strict_lower).to_a # => [3]
Iterate over the elements of this vector
Iterate over the elements of this vector and v
in conjunction.
Searches key
in @stack for id
hash and returns or yields the result.
Searches key
in @stack for id
hash and returns or yields the result.
Iterates the given block over all prime numbers.
ubound
Optional. An arbitrary positive number. The upper bound of enumeration. The method enumerates prime numbers infinitely if ubound
is nil.
generator
Optional. An implementation of pseudo-prime generator.
An evaluated value of the given block at the last time. Or an enumerator which is compatible to an Enumerator
if no block given.
Calls block
once for each prime number, passing the prime as a parameter.
ubound
Upper bound of prime numbers. The iterator stops after it yields all prime numbers p <= ubound
.
This method is just like PStore#[]
, save that you may also provide a default value for the object. In the event the specified name is not found in the data store, your default will be returned instead. If you do not specify a default, PStore::Error
will be raised if the object is not found.
WARNING: This method is only valid in a PStore#transaction
. It will raise PStore::Error
if called at any other time.
Returns a string containing the character represented by the int
‘s value according to encoding
.
65.chr #=> "A" 230.chr #=> "\346" 255.chr(Encoding::UTF_8) #=> "\303\277"
Calls the given block once for each element in the set, passing the element as parameter. Returns an enumerator if no block is given.
Equivalent to ($_.dup).chop!
, except nil
is never returned. See String#chop!
. Available only when -p/-n command line option specified.