D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
proc
/
self
/
root
/
opt
/
alt
/
ruby34
/
share
/
ri
/
system
/
ARGF
/
Filename :
cdesc-ARGF.ri
back
Copy
U:RDoc::NormalClass[iI" ARGF:ET@I"Object;To:RDoc::Markup::Document:@parts[o;;[aS:RDoc::Markup::Heading: leveli: textI"\ARGF and +ARGV+;To:RDoc::Markup::BlankLine o:RDoc::Markup::Paragraph;[I"EThe \ARGF object works with the array at global variable +ARGV+ ;TI"Lto make <tt>$stdin</tt> and file streams available in the Ruby program:;T@o:RDoc::Markup::List: @type:BULLET:@items[o:RDoc::Markup::ListItem:@label0;[ o; ;[I"D**ARGV** may be thought of as the <b>argument vector</b> array.;T@o; ;[I"CInitially, it contains the command-line arguments and options ;TI"*that are passed to the Ruby program; ;TI"3the program can modify that array as it likes.;T@o;;0;[ o; ;[I"D**ARGF** may be thought of as the <b>argument files</b> object.;T@o; ;[ I"CIt can access file streams and/or the <tt>$stdin</tt> stream, ;TI"'based on what it finds in +ARGV+. ;TI"9This provides a convenient way for the command line ;TI"3to specify streams for a Ruby program to read.;T@S; ; i;I"Reading;T@o; ;[I"+\ARGF may read from _source_ streams, ;TI"Jwhich at any particular time are determined by the content of +ARGV+.;T@S; ; i;I"Simplest Case;T@o; ;[I"VWhen the <i>very first</i> \ARGF read occurs with an empty +ARGV+ (<tt>[]</tt>), ;TI"#the source is <tt>$stdin</tt>:;T@o;;;;[o;;0;[o; ;[I"\File +t.rb+:;T@o:RDoc::Markup::Verbatim;[I"p ['ARGV', ARGV] ;TI" p ['ARGF.read', ARGF.read] ;T:@format0o;;0;[o; ;[I"Commands and outputs ;TI"B(see below for the content of files +foo.txt+ and +bar.txt+):;T@o;;[I"7$ echo "Open the pod bay doors, Hal." | ruby t.rb ;TI"["ARGV", []] ;TI"5["ARGF.read", "Open the pod bay doors, Hal.\n"] ;TI" ;TI"'$ cat foo.txt bar.txt | ruby t.rb ;TI"["ARGV", []] ;TI"A["ARGF.read", "Foo 0\nFoo 1\nBar 0\nBar 1\nBar 2\nBar 3\n"] ;T;0S; ; i;I"About the Examples;T@o; ;[I"NMany examples here assume the existence of files +foo.txt+ and +bar.txt+:;T@o;;[ I"$ cat foo.txt ;TI"Foo 0 ;TI"Foo 1 ;TI"$ cat bar.txt ;TI"Bar 0 ;TI"Bar 1 ;TI"Bar 2 ;TI"Bar 3 ;T;0S; ; i;I"Sources in +ARGV+;T@o; ;[I"RFor any \ARGF read _except_ the {simplest case}[rdoc-ref:ARGF@Simplest+Case] ;TI"T(that is, _except_ for the <i>very first</i> \ARGF read with an empty +ARGV+), ;TI"%the sources are found in +ARGV+.;T@o; ;[I"L\ARGF assumes that each element in array +ARGV+ is a potential source, ;TI"and is one of:;T@o;;;;[o;;0;[o; ;[I">The string path to a file that may be opened as a stream.;To;;0;[o; ;[I"@The character <tt>'-'</tt>, meaning stream <tt>$stdin</tt>.;T@o; ;[I"-Each element that is _not_ one of these ;TI"Eshould be removed from +ARGV+ before \ARGF accesses that source.;T@o; ;[I"In the following example:;T@o;;;;[o;;0;[o; ;[I"LFilepaths +foo.txt+ and +bar.txt+ may be retained as potential sources.;To;;0;[o; ;[I"DOptions <tt>--xyzzy</tt> and <tt>--mojo</tt> should be removed.;T@o; ;[I" Example:;T@o;;;;[o;;0;[o; ;[I"\File +t.rb+:;T@o;;[I"D# Print arguments (and options, if any) found on command line. ;TI"p ['ARGV', ARGV] ;T;0o;;0;[o; ;[I"Command and output:;T@o;;[I"0$ ruby t.rb --xyzzy --mojo foo.txt bar.txt ;TI";["ARGV", ["--xyzzy", "--mojo", "foo.txt", "bar.txt"]] ;T;0o; ;[I"K\ARGF's stream access considers the elements of +ARGV+, left to right:;T@o;;;;[o;;0;[o; ;[I"\File +t.rb+:;T@o;;[I"p "ARGV: #{ARGV}" ;TI"Jp "Line: #{ARGF.read}" # Read everything from all specified streams. ;T;0o;;0;[o; ;[I"Command and output:;T@o;;[I"!$ ruby t.rb foo.txt bar.txt ;TI"("ARGV: [\"foo.txt\", \"bar.txt\"]" ;TI"8"Read: Foo 0\nFoo 1\nBar 0\nBar 1\nBar 2\nBar 3\n" ;T;0o; ;[I"7Because the value at +ARGV+ is an ordinary array, ;TI"Dyou can manipulate it to control which sources \ARGF considers:;T@o;;;;[o;;0;[o; ;[I"\If you remove an element from +ARGV+, \ARGF will not consider the corresponding source.;To;;0;[o; ;[I"SIf you add an element to +ARGV+, \ARGF will consider the corresponding source.;T@o; ;[I"REach element in +ARGV+ is removed when its corresponding source is accessed; ;TI"=when all sources have been accessed, the array is empty:;T@o;;;;[o;;0;[o; ;[I"\File +t.rb+:;T@o;;[ I"$until ARGV.empty? && ARGF.eof? ;TI" p "ARGV: #{ARGV}" ;TI"O p "Line: #{ARGF.readline}" # Read each line from each specified stream. ;TI" end ;T;0o;;0;[o; ;[I"Command and output:;T@o;;[I"!$ ruby t.rb foo.txt bar.txt ;TI"("ARGV: [\"foo.txt\", \"bar.txt\"]" ;TI""Line: Foo 0\n" ;TI""ARGV: [\"bar.txt\"]" ;TI""Line: Foo 1\n" ;TI""ARGV: [\"bar.txt\"]" ;TI""Line: Bar 0\n" ;TI""ARGV: []" ;TI""Line: Bar 1\n" ;TI""ARGV: []" ;TI""Line: Bar 2\n" ;TI""ARGV: []" ;TI""Line: Bar 3\n" ;T;0S; ; i ;I"Filepaths in +ARGV+;T@o; ;[I"RThe +ARGV+ array may contain filepaths the specify sources for \ARGF reading.;T@o; ;[I"IThis program prints what it reads from files at the paths specified ;TI"on the command line:;T@o;;;;[o;;0;[o; ;[I"\File +t.rb+:;T@o;;[I"p ['ARGV', ARGV] ;TI"># Read and print all content from the specified sources. ;TI" p ['ARGF.read', ARGF.read] ;T;0o;;0;[o; ;[I"Command and output:;T@o;;[I"!$ ruby t.rb foo.txt bar.txt ;TI"!["ARGV", [foo.txt, bar.txt] ;TI"A["ARGF.read", "Foo 0\nFoo 1\nBar 0\nBar 1\nBar 2\nBar 3\n"] ;T;0S; ; i ;I")Specifying <tt>$stdin</tt> in +ARGV+;T@o; ;[I"PTo specify stream <tt>$stdin</tt> in +ARGV+, us the character <tt>'-'</tt>:;T@o;;;;[o;;0;[o; ;[I"\File +t.rb+:;T@o;;[I"p ['ARGV', ARGV] ;TI" p ['ARGF.read', ARGF.read] ;T;0o;;0;[o; ;[I"Command and output:;T@o;;[I"9$ echo "Open the pod bay doors, Hal." | ruby t.rb - ;TI"["ARGV", ["-"]] ;TI"5["ARGF.read", "Open the pod bay doors, Hal.\n"] ;T;0o; ;[I"PWhen no character <tt>'-'</tt> is given, stream <tt>$stdin</tt> is ignored ;TI"(exception: ;TI"Qsee {Specifying $stdin in ARGV}[rdoc-ref:ARGF@Specifying+-24stdin+in+ARGV]):;T@o;;;;[o;;0;[o; ;[I"Command and output:;T@o;;[I"G$ echo "Open the pod bay doors, Hal." | ruby t.rb foo.txt bar.txt ;TI"("ARGV: [\"foo.txt\", \"bar.txt\"]" ;TI"8"Read: Foo 0\nFoo 1\nBar 0\nBar 1\nBar 2\nBar 3\n" ;T;0S; ; i ;I"'Mixtures and Repetitions in +ARGV+;T@o; ;[I"FFor an \ARGF reader, +ARGV+ may contain any mixture of filepaths ;TI"7and character <tt>'-'</tt>, including repetitions.;T@S; ; i ;I"Modifications to +ARGV+;T@o; ;[I"NThe running Ruby program may make any modifications to the +ARGV+ array; ;TI"7the current value of +ARGV+ affects \ARGF reading.;T@S; ; i ;I"Empty +ARGV+;T@o; ;[I"DFor an empty +ARGV+, an \ARGF read method either returns +nil+ ;TI">or raises an exception, depending on the specific method.;T@S; ; i;I"More Read Methods;T@o; ;[ I"FAs seen above, method ARGF#read reads the content of all sources ;TI"into a single string. ;TI"DOther \ARGF methods provide other ways to access that content; ;TI"these include:;T@o;;;;[ o;;0;[o; ;[I"2Byte access: #each_byte, #getbyte, #readbyte.;To;;0;[o; ;[I"4Character access: #each_char, #getc, #readchar.;To;;0;[o; ;[I"'Codepoint access: #each_codepoint.;To;;0;[o; ;[I";Line access: #each_line, #gets, #readline, #readlines.;To;;0;[o; ;[I"8Source access: #read, #read_nonblock, #readpartial.;T@S; ; i;I"About \Enumerable;T@o; ;[I"'\ARGF includes module Enumerable. ;TI"\Virtually all methods in \Enumerable call method <tt>#each</tt> in the including class.;T@o; ;[ I"O<b>Note well</b>: In \ARGF, method #each returns data from the _sources_, ;TI"_not_ from +ARGV+; ;TI"_therefore, for example, <tt>ARGF#entries</tt> returns an array of lines from the sources, ;TI"-not an array of the strings from +ARGV+:;T@o;;;;[o;;0;[o; ;[I"\File +t.rb+:;T@o;;[I"p ['ARGV', ARGV] ;TI"&p ['ARGF.entries', ARGF.entries] ;T;0o;;0;[o; ;[I"Command and output:;T@o;;[I"!$ ruby t.rb foo.txt bar.txt ;TI"&["ARGV", ["foo.txt", "bar.txt"]] ;TI"Z["ARGF.entries", ["Foo 0\n", "Foo 1\n", "Bar 0\n", "Bar 1\n", "Bar 2\n", "Bar 3\n"]] ;T;0S; ; i;I"Writing;T@o; ;[I"*If <i>inplace mode</i> is in effect, ;TI"(\ARGF may write to target streams, ;TI"Hwhich at any particular time are determined by the content of ARGV.;T@o; ;[I" Methods about inplace mode:;T@o;;;;[o;;0;[o; ;[I"#inplace_mode;To;;0;[o; ;[I"#inplace_mode=;To;;0;[o; ;[I"#to_write_io;T@o; ;[I"Methods for writing:;T@o;;;;[ o;;0;[o; ;[I"#print;To;;0;[o; ;[I"#printf;To;;0;[o; ;[I" #putc;To;;0;[o; ;[I" #puts;To;;0;[o; ;[I"#write;T: @fileI" io.c;T:0@omit_headings_from_table_of_contents_below0;0;0[ [ [[I"Enumerable;To;;[ ;@�;0I" io.c;T[[I" class;T[[:public[ [:protected[ [:private[ [I" instance;T[[;[7[I" argv;T@�[I"binmode;T@�[I" binmode?;T@�[I" close;T@�[I"closed?;T@�[I" each;T@�[I"each_byte;T@�[I"each_char;T@�[I"each_codepoint;T@�[I"each_line;T@�[I"eof;T@�[I" eof?;T@�[I"external_encoding;T@�[I" file;T@�[I" filename;T@�[I"fileno;T@�[I"getbyte;T@�[I" getc;T@�[I" gets;T@�[I"inplace_mode;T@�[I"inplace_mode=;T@�[I"inspect;T@�[I"internal_encoding;T@�[I"lineno;T@�[I"lineno=;T@�[I" path;T@�[I"pos;T@�[I" pos=;T@�[I" print;T@�[I"printf;T@�[I" putc;T@�[I" puts;T@�[I" read;T@�[I"read_nonblock;T@�[I" readbyte;T@�[I" readchar;T@�[I" readline;T@�[I"readlines;T@�[I"readpartial;T@�[I"rewind;T@�[I" seek;T@�[I"set_encoding;T@�[I" skip;T@�[I" tell;T@�[I" to_a;T@�[I" to_i;T@�[I" to_io;T@�[I" to_s;T@�[I"to_write_io;T@�[I" write;T@�[;[ [;[ [ [U:RDoc::Context::Section[i 0o;;[ ;0;0[@�@�cRDoc::TopLevel