Class: ARGF
- Inherits:
- NSObject show all
- Includes:
- Enumerable
Instance Method Summary (collapse)
-
- argv
Returns the ARGV array, which contains the arguments passed to your script, one per element.
-
- binmode
Puts ARGF into binary mode.
-
- binmode?
Returns true if ARGF is being read in binary mode; false otherwise.
- - bytes
- - chars
-
- close
Closes the current file and skips to the next in the stream.
-
- closed?
Returns true if the current file has been closed; false otherwise.
- - each
- - each_byte
- - each_char
- - each_line
-
- eof
Returns true if the current file in ARGF is at end of file, i.e.
-
- eof?
Returns true if the current file in ARGF is at end of file, i.e.
- - external_encoding
- - file
-
- filename
Returns the current filename.
-
- fileno
Returns an integer representing the numeric file descriptor for the current file.
-
- getbyte
Gets the next 8-bit byte (0..255) from ARGF.
- - getc
-
- gets
Returns the next line from the current file in ARGF.
-
- initialize
constructor
:nodoc:.
-
- initialize_copy
:nodoc:.
-
- inplace_mode
Returns the file extension appended to the names of modified files under inplace-edit mode.
-
- inplace_mode=
Sets the filename extension for inplace editing mode to the given String.
- - internal_encoding
-
- lineno
Returns the current line number of ARGF as a whole.
- - lineno=
- - lines
-
- path
Returns the current filename.
-
- pos
Returns the current offset (in bytes) of the current file in ARGF.
-
- pos=
Seeks to the position given by position (in bytes) in ARGF.
-
- read
Reads length bytes from ARGF.
- - readbyte
- - readchar
-
- readline
Returns the next line from the current file in ARGF.
-
- readlines
ARGF.to_a(sep=$/) -> array.
-
- readpartial
Reads at most maxlen bytes from the ARGF stream.
-
- rewind
Positions the current file to the beginning of input, resetting ARGF.lineno to zero.
- - seek
-
- set_encoding
If single argument is specified, strings read from ARGF are tagged with the encoding specified.
-
- skip
Sets the current file to the next file in ARGV.
-
- tell
Returns the current offset (in bytes) of the current file in ARGF.
-
- to_a
ARGF.to_a(sep=$/) -> array.
-
- to_i
Returns an integer representing the numeric file descriptor for the current file.
-
- to_io
Returns an IO object representing the current file.
-
- to_s
Returns "ARGF".
Methods included from Enumerable
#_count, #all?, #any?, #collect, #collect_concat, #count, #cycle, #detect, #drop, #drop_while, #each_cons, #each_entry, #each_slice, #each_with_index, #each_with_object, #entries, #find, #find_all, #find_index, #first, #flat_map, #grep, #group_by, #include?, #inject, #map, #max, #max_by, #member?, #min, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reduce, #reject, #reverse_each, #select, #sort, #sort_by, #take, #take_while, #zip
Methods inherited from NSObject
#!, #!=, #!~, #, #==, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, alloc, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #eql?, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version
Constructor Details
- (Object) initialize
:nodoc:
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class NSObject
Instance Method Details
- (Object) argv
Returns the ARGV array, which contains the arguments passed to your script, one per element.
For example:
$ ruby argf.rb -v glark.txt
ARGF.argv #=> ["-v", "glark.txt"]
- (Object) binmode
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.
- (Boolean) binmode?
- (Object) bytes {|byte| ... } - (Enumerator) bytes
ARGF.each_byte {|byte| block } -> ARGF
ARGF.each_byte -> an_enumerator
Iterates over each byte of each file in ARGV. A byte is returned as a Fixnum in the range 0..255.
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 byte of the first file has been returned, the first byte of the second file is returned. The ARGF.filename method can be used to determine the filename of the current byte.
If no block is given, an enumerator is returned instead.
For example:
ARGF.bytes.to_a #=> [35, 32, ... 95, 10]
- (Object) chars {|char| ... } - (Enumerator) chars
ARGF.each_char {|char| block } -> ARGF
ARGF.each_char -> an_enumerator
Iterates over each character of each file in ARGF.
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 character of the first file has been returned, the first character of the second file is returned. The ARGF.filename method can be used to determine the name of the file in which the current character appears.
If no block is given, an enumerator is returned instead.
- (Object) close
Closes the current file and skips to the next in the stream. Trying to close a file that has already been closed causes an IOError to be raised.
For example:
$ ruby argf.rb foo
ARGF.filename #=> "foo"
ARGF.close
ARGF.filename #=> "bar"
ARGF.close
ARGF.close #=> closed stream (IOError)
- (Boolean) closed?
Returns true if the current file has been closed; false otherwise. Use ARGF.close to actually close the current file.
- (Object) each(sep = $/) {|line| ... } - (Object) each(sep = $/, limit) {|line| ... } - (Enumerator) each(...)
ARGF.each_line(sep=$/) {|line| block } -> ARGF
ARGF.each_line(sep=$/,limit) {|line| block } -> ARGF
ARGF.each_line(...) -> an_enumerator
ARGF.lines(sep=$/) {|line| block } -> ARGF
ARGF.lines(sep=$/,limit) {|line| block } -> ARGF
ARGF.lines(...) -> an_enumerator
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 a Fixnum 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 and line number, respectively, of the current line.
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.lines do |line|
puts ARGF.filename if ARGF.lineno == 1
puts "#{ARGF.lineno}: #{line}"
end
- (Object) bytes {|byte| ... } - (Enumerator) bytes
ARGF.each_byte {|byte| block } -> ARGF
ARGF.each_byte -> an_enumerator
Iterates over each byte of each file in ARGV. A byte is returned as a Fixnum in the range 0..255.
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 byte of the first file has been returned, the first byte of the second file is returned. The ARGF.filename method can be used to determine the filename of the current byte.
If no block is given, an enumerator is returned instead.
For example:
ARGF.bytes.to_a #=> [35, 32, ... 95, 10]
- (Object) chars {|char| ... } - (Enumerator) chars
ARGF.each_char {|char| block } -> ARGF
ARGF.each_char -> an_enumerator
Iterates over each character of each file in ARGF.
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 character of the first file has been returned, the first character of the second file is returned. The ARGF.filename method can be used to determine the name of the file in which the current character appears.
If no block is given, an enumerator is returned instead.
- (Object) each(sep = $/) {|line| ... } - (Object) each(sep = $/, limit) {|line| ... } - (Enumerator) each(...)
ARGF.each_line(sep=$/) {|line| block } -> ARGF
ARGF.each_line(sep=$/,limit) {|line| block } -> ARGF
ARGF.each_line(...) -> an_enumerator
ARGF.lines(sep=$/) {|line| block } -> ARGF
ARGF.lines(sep=$/,limit) {|line| block } -> ARGF
ARGF.lines(...) -> an_enumerator
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 a Fixnum 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 and line number, respectively, of the current line.
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.lines do |line|
puts ARGF.filename if ARGF.lineno == 1
puts "#{ARGF.lineno}: #{line}"
end
- (Boolean) eof? - (Boolean) eof
- (Boolean) eof? - (Boolean) eof
- (Encoding) external_encoding
Returns the external encoding for files read from ARGF as an Encoding object. The external encoding is the encoding of the text as stored in a file. Contrast with ARGF.internal_encoding, which is the encoding used to represent this text within Ruby.
To set the external encoding use ARGF.set_encoding.
For example:
ARGF.external_encoding #=> #<Encoding:UTF-8>
- (File object) file
- (String) filename - (String) path
Returns the current filename. "-" is returned when the current file is STDIN.
For example:
$ echo "foo" > foo
$ echo "bar" >
$ echo "glark" > glark
$ ruby argf.rb foo glark
ARGF.filename #=> "foo"
ARGF.read(5) #=> "foo\nb"
ARGF.filename #=> "bar"
ARGF.skip
ARGF.filename #=> "glark"
- (Fixnum) fileno - (Fixnum) to_i
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
- (Fixnum?) getbyte
Gets the next 8-bit byte (0..255) from ARGF. Returns nil if called at the end of the stream.
For example:
$ echo "foo" > file
$ ruby argf.rb file
ARGF.getbyte #=> 102
ARGF.getbyte #=> 111
ARGF.getbyte #=> 111
ARGF.getbyte #=> 10
ARGF.getbyte #=> nil
- (String?) getc
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
- (String) gets(sep = $/) - (String) gets(limit) - (String) gets(sep, limit)
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.
- (Object) initialize_copy
:nodoc:
- (String) inplace_mode
Returns the file extension appended to the names of modified files under inplace-edit mode. This value can be set using ARGF.inplace_mode= or passing the -i switch to the Ruby binary.
- (Object) inplace_mode=(ext)
Sets the filename extension for inplace editing mode to the given String. Each file being edited has this value appended to its filename. The modified file is saved under this new name.
For example:
$ ruby argf.rb file.txt
ARGF.inplace_mode = '.bak'
ARGF.lines do |line|
print line.sub("foo","bar")
end
Each line of file.txt has the first occurrence of "foo" replaced with "bar", then the new line is written out to file.txt.bak.
- (Encoding) internal_encoding
Returns the internal encoding for strings read from ARGF as an Encoding object.
If ARGF.set_encoding has been called with two encoding names, the second is returned. Otherwise, if Encoding.default_external has been set, that value is returned. Failing that, if a default external encoding was specified on the command-line, that value is used. If the encoding is unknown, nil is returned.
- (Integer) lineno
- (nil) lineno=(number)
Sets the line number of ARGF as a whole to the given Integer.
ARGF sets the line number automatically as you read data, so normally you will not need to set it explicitly. To access the current line number use ARGF.lineno.
For example:
ARGF.lineno #=> 0
ARGF.readline #=> "This is line 1\n"
ARGF.lineno #=> 1
ARGF.lineno = 0 #=> nil
ARGF.lineno #=> 0
- (Object) each(sep = $/) {|line| ... } - (Object) each(sep = $/, limit) {|line| ... } - (Enumerator) each(...)
ARGF.each_line(sep=$/) {|line| block } -> ARGF
ARGF.each_line(sep=$/,limit) {|line| block } -> ARGF
ARGF.each_line(...) -> an_enumerator
ARGF.lines(sep=$/) {|line| block } -> ARGF
ARGF.lines(sep=$/,limit) {|line| block } -> ARGF
ARGF.lines(...) -> an_enumerator
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 a Fixnum 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 and line number, respectively, of the current line.
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.lines do |line|
puts ARGF.filename if ARGF.lineno == 1
puts "#{ARGF.lineno}: #{line}"
end
- (String) filename - (String) path
Returns the current filename. "-" is returned when the current file is STDIN.
For example:
$ echo "foo" > foo
$ echo "bar" >
$ echo "glark" > glark
$ ruby argf.rb foo glark
ARGF.filename #=> "foo"
ARGF.read(5) #=> "foo\nb"
ARGF.filename #=> "bar"
ARGF.skip
ARGF.filename #=> "glark"
- (Integer) tell - (Integer) pos
Returns the current offset (in bytes) of the current file in ARGF.
ARGF.pos #=> 0
ARGF.gets #=> "This is line one\n"
ARGF.pos #=> 17
- (Integer) pos=(position)
Seeks to the position given by position (in bytes) in ARGF.
For example:
ARGF.pos = 17
ARGF.gets #=> "This is line two\n"
- (String?) read([length [, buffer]])
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 it is a positive integer, read tries to read at most length bytes. It returns nil if an EOF was encountered before anything could be read. Fewer than length bytes may be returned if an EOF is encountered during the read.
If length is omitted or is nil, it reads until EOF. A String is returned even if EOF is encountered before any data is read.
If length is zero, it returns _""_.
If the optional buffer argument is present, it must reference a String, which will receive the data.
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 fread() function in C. If you need the behavior like read(2) system call, consider ARGF.readpartial.
- (Fixnum) readbyte
Reads the next 8-bit byte from ARGF and returns it as a Fixnum. Raises an EOFError after the last byte of the last file has been read.
For example:
$ echo "foo" > file
$ ruby argf.rb file
ARGF.readbyte #=> 102
ARGF.readbyte #=> 111
ARGF.readbyte #=> 111
ARGF.readbyte #=> 10
ARGF.readbyte #=> end of file reached (EOFError)
- (String?) readchar
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)
- (String) readline(sep = $/) - (String) readline(limit) - (String) readline(sep, limit)
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.
- (String) readpartial(maxlen) - (Object) readpartial(maxlen, outbuf)
Reads at most maxlen bytes from the ARGF stream. It blocks only if ARGF has no data immediately available. If the optional outbuf argument is present, it must reference a String, which will receive the data. It raises EOFError on end of file.
readpartial is designed for streams such as pipes, sockets, and ttys. It blocks only when no data is immediately available. This means that it blocks only when following all conditions hold:
-
The byte buffer in the IO object is empty.
-
The content of the stream is empty.
-
The stream has not reached EOF.
When readpartial blocks, it waits for data or EOF. If some data is read, readpartial returns with the data. If EOF is reached, readpartial raises an EOFError.
When readpartial doesn't block, it returns or raises immediately. If the byte buffer is not empty, it returns the data in the buffer. Otherwise, if the stream has some content, it returns the data in the stream. If the stream reaches EOF an EOFError is raised.
- (0) rewind
Positions the current file to the beginning of input, resetting ARGF.lineno to zero.
ARGF.readline #=> "This is line one\n"
ARGF.rewind #=> 0
ARGF.lineno #=> 0
ARGF.readline #=> "This is line one\n"
- (0) seek(amount, whence = IO::SEEK_SET)
- (Object) set_encoding(ext_enc) - (Object) set_encoding("ext_enc:int_enc") - (Object) set_encoding(ext_enc, int_enc) - (Object) set_encoding("ext_enc:int_enc", opt) - (Object) set_encoding(ext_enc, int_enc, opt)
If single argument is specified, strings read from ARGF are tagged with the encoding specified.
If two encoding names separated by a colon are given, e.g. "ascii:utf-8", the read string is converted from the first encoding (external encoding) to the second encoding (internal encoding), then tagged with the second encoding.
If two arguments are specified, they must be encoding objects or encoding names. Again, the first specifies the external encoding; the second specifies the internal encoding.
If the external encoding and the internal encoding are specified, the optional Hash argument can be used to adjust the conversion process. The structure of this hash is explained in the String#encode documentation.
For example:
ARGF.set_encoding('ascii') # Tag the input as US-ASCII text
ARGF.set_encoding(Encoding::UTF_8) # Tag the input as UTF-8 text
ARGF.set_encoding('utf-8','ascii') # Transcode the input from US-ASCII
# to UTF-8.
- (Object) skip
Sets the current file to the next file in ARGV. If there aren't any more files it has no effect.
For example:
$ ruby argf.rb foo
ARGF.filename #=> "foo"
ARGF.skip
ARGF.filename #=> "bar"
- (Integer) tell - (Integer) pos
Returns the current offset (in bytes) of the current file in ARGF.
ARGF.pos #=> 0
ARGF.gets #=> "This is line one\n"
ARGF.pos #=> 17
- (Fixnum) fileno - (Fixnum) to_i
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