Truncates the buffer string to at most integer bytes. The strio must be opened for writing.
Set
the scan pointer to the end of the string and clear matching data.
Appends str
to the string being scanned. This method does not affect scan pointer.
s = StringScanner.new("Fri Dec 12 1975 14:39") s.scan(/Fri /) s << " +1000 GMT" s.string # -> "Fri Dec 12 1975 14:39 +1000 GMT" s.scan(/Dec/) # -> "Dec"
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
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.
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 the type library file path.
tlib = WIN32OLE_TYPELIB.new('Microsoft Excel 9.0 Object Library') puts tlib.path #-> 'C:\...\EXCEL9.OLB'
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
Adds the contents of the given hashes to the receiver.
If no block is given, entries with duplicate keys are overwritten with the values from each other_hash
successively, otherwise the value for each duplicate key is determined by calling the block with the key, its value in the receiver and its value in each other_hash
.
h1 = { "a" => 100, "b" => 200 } h1.merge! #=> {"a"=>100, "b"=>200} h1 #=> {"a"=>100, "b"=>200} h1 = { "a" => 100, "b" => 200 } h2 = { "b" => 246, "c" => 300 } h1.merge!(h2) #=> {"a"=>100, "b"=>246, "c"=>300} h1 #=> {"a"=>100, "b"=>246, "c"=>300} h1 = { "a" => 100, "b" => 200 } h2 = { "b" => 246, "c" => 300 } h3 = { "b" => 357, "d" => 400 } h1.merge!(h2, h3) #=> {"a"=>100, "b"=>357, "c"=>300, "d"=>400} h1 #=> {"a"=>100, "b"=>357, "c"=>300, "d"=>400} h1 = { "a" => 100, "b" => 200 } h2 = { "b" => 246, "c" => 300 } h3 = { "b" => 357, "d" => 400 } h1.merge!(h2, h3) {|key, v1, v2| v1 } #=> {"a"=>100, "b"=>200, "c"=>300, "d"=>400} h1 #=> {"a"=>100, "b"=>200, "c"=>300, "d"=>400}
Hash#update
is an alias for Hash#merge!
.
Returns a new array that is a one-dimensional flattening of this hash. That is, for every key or value that is an array, extract its elements into the new array. Unlike Array#flatten
, this method does not flatten recursively by default. The optional level argument determines the level of recursion to flatten.
a = {1=> "one", 2 => [2,"two"], 3 => "three"} a.flatten # => [1, "one", 2, [2, "two"], 3, "three"] a.flatten(2) # => [1, "one", 2, 2, "two", 3, "three"]
Yields each environment variable name
and value
.
If no block is given an Enumerator
is returned.
Adds the contents of hash
to the environment variables. If no block is specified entries with duplicate keys are overwritten, otherwise the value of each duplicate name is determined by calling the block with the key, its value from the environment and its value from the hash.
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
This is a deprecated alias for each_char
.
Reads the next character from ARGF
and returns it as a String
. Returns nil
at the end of the stream.
ARGF
treats the files named on the command line as a single file created by concatenating their contents. After returning the last character of the first file, it returns the first character of the second file, and so on.
For example:
$ echo "foo" > file $ ruby argf.rb file ARGF.getc #=> "f" ARGF.getc #=> "o" ARGF.getc #=> "o" ARGF.getc #=> "\n" ARGF.getc #=> nil ARGF.getc #=> nil
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)
Puts ARGF
into binary mode. Once a stream is in binary mode, it cannot be reset to non-binary mode. This option has the following effects:
Newline conversion is disabled.
Encoding
conversion is disabled.
Content is treated as ASCII-8BIT.
Returns true if ARGF
is being read in binary mode; false otherwise. To enable binary mode use ARGF.binmode
.
For example:
ARGF.binmode? #=> false ARGF.binmode ARGF.binmode? #=> true
If obj is Numeric
, write the character whose code is the least-significant byte of obj. If obj is String
, write the first character of obj to ios. Otherwise, raise TypeError
.
$stdout.putc "A" $stdout.putc 65
produces:
AA
Returns the current filename. “-” is returned when the current file is STDIN.
For example:
$ echo "foo" > foo $ echo "bar" > bar $ echo "glark" > glark $ ruby argf.rb foo bar glark ARGF.filename #=> "foo" ARGF.read(5) #=> "foo\nb" ARGF.filename #=> "bar" ARGF.skip ARGF.filename #=> "glark"
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.
This method wraps a String
you provide, or an empty default String
, in a CSV
object which is passed to the provided block. You can use the block to append CSV
rows to the String
and when the block exits, the final String
will be returned.
Note that a passed String
is modified by this method. Call dup() before passing if you need a new String
.
The options
parameter can be anything CSV::new()
understands. This method understands an additional :encoding
parameter when not passed a String
to set the base Encoding
for the output. CSV
needs this hint if you plan to output non-ASCII compatible data.