@@ -14700,47 +14700,252 @@ set_LAST_READ_LINE(VALUE val, ID _x, VALUE *_y)
14700
14700
/*
14701
14701
* Document-class: ARGF
14702
14702
*
14703
- * ARGF is a stream designed for use in scripts that process files given as
14704
- * command-line arguments or passed in via STDIN.
14703
+ * == \ARGF and +ARGV+
14705
14704
*
14706
- * The arguments passed to your script are stored in the +ARGV+ Array, one
14707
- * argument per element. ARGF assumes that any arguments that aren't
14708
- * filenames have been removed from +ARGV+. For example:
14705
+ * The \ARGF object works with the array at global variable +ARGV+
14706
+ * to make <tt>$stdin</tt> and file streams available in the Ruby program:
14709
14707
*
14710
- * $ ruby argf.rb --verbose file1 file2
14708
+ * - **ARGV** may be thought of as the <b>argument vector</b> array.
14711
14709
*
14712
- * ARGV #=> ["--verbose", "file1", "file2"]
14713
- * option = ARGV.shift #=> "--verbose"
14714
- * ARGV #=> ["file1", "file2"]
14710
+ * Initially, it contains the command-line arguments and options
14711
+ * that are passed to the Ruby program;
14712
+ * the program can modify that array as it likes.
14715
14713
*
14716
- * You can now use ARGF to work with a concatenation of each of these named
14717
- * files. For instance, ARGF.read will return the contents of _file1_
14718
- * followed by the contents of _file2_.
14714
+ * - **ARGF** may be thought of as the <b>argument files</b> object.
14719
14715
*
14720
- * After a file in +ARGV+ has been read ARGF removes it from the Array.
14721
- * Thus, after all files have been read +ARGV+ will be empty.
14716
+ * It can access file streams and/or the <tt>$stdin</tt> stream,
14717
+ * based on what it finds in +ARGV+.
14718
+ * This provides a convenient way for the command line
14719
+ * to specify streams for a Ruby program to read.
14722
14720
*
14723
- * You can manipulate +ARGV+ yourself to control what ARGF operates on. If
14724
- * you remove a file from +ARGV+, it is ignored by ARGF; if you add files to
14725
- * +ARGV+, they are treated as if they were named on the command line. For
14726
- * example:
14721
+ * == Reading
14727
14722
*
14728
- * ARGV.replace ["file1"]
14729
- * ARGF.readlines # Returns the contents of file1 as an Array
14730
- * ARGV #=> []
14731
- * ARGV.replace ["file2", "file3"]
14732
- * ARGF.read # Returns the contents of file2 and file3
14723
+ * \ARGF may read from _source_ streams,
14724
+ * which at any particular time are determined by the content of +ARGV+.
14733
14725
*
14734
- * If +ARGV+ is empty, ARGF acts as if it contained <tt>"-"</tt> that
14735
- * makes ARGF read from STDIN, i.e. the data piped or typed to your
14736
- * script. For example:
14726
+ * === Simplest Case
14737
14727
*
14738
- * $ echo "glark" | ruby -e 'p ARGF.read'
14739
- * "glark\n"
14728
+ * When the <i>very first</i> \ARGF read occurs with an empty +ARGV+ (<tt>[]</tt>),
14729
+ * the source is <tt>$stdin</tt>:
14730
+ *
14731
+ * - \File +t.rb+:
14732
+ *
14733
+ * p ['ARGV', ARGV]
14734
+ * p ['ARGF.read', ARGF.read]
14735
+ *
14736
+ * - Commands and outputs
14737
+ * (see below for the content of files +foo.txt+ and +bar.txt+):
14738
+ *
14739
+ * $ echo "Open the pod bay doors, Hal." | ruby t.rb
14740
+ * ["ARGV", []]
14741
+ * ["ARGF.read", "Open the pod bay doors, Hal.\n"]
14742
+ *
14743
+ * $ cat foo.txt bar.txt | ruby t.rb
14744
+ * ["ARGV", []]
14745
+ * ["ARGF.read", "Foo 0\nFoo 1\nBar 0\nBar 1\nBar 2\nBar 3\n"]
14746
+ *
14747
+ * === About the Examples
14748
+ *
14749
+ * Many examples here assume the existence of files +foo.txt+ and +bar.txt+:
14750
+ *
14751
+ * $ cat foo.txt
14752
+ * Foo 0
14753
+ * Foo 1
14754
+ * $ cat bar.txt
14755
+ * Bar 0
14756
+ * Bar 1
14757
+ * Bar 2
14758
+ * Bar 3
14759
+ *
14760
+ * === Sources in +ARGV+
14761
+ *
14762
+ * For any \ARGF read _except_ the {simplest case}[rdoc-ref:ARGF@Simplest+Case]
14763
+ * (that is, _except_ for the <i>very first</i> \ARGF read with an empty +ARGV+),
14764
+ * the sources are found in +ARGV+.
14765
+ *
14766
+ * \ARGF assumes that each element in array +ARGV+ is a potential source,
14767
+ * and is one of:
14768
+ *
14769
+ * - The string path to a file that may be opened as a stream.
14770
+ * - The character <tt>'-'</tt>, meaning stream <tt>$stdin</tt>.
14771
+ *
14772
+ * Each element that is _not_ one of these
14773
+ * should be removed from +ARGV+ before \ARGF accesses that source.
14774
+ *
14775
+ * In the following example:
14776
+ *
14777
+ * - Filepaths +foo.txt+ and +bar.txt+ may be retained as potential sources.
14778
+ * - Options <tt>--xyzzy</tt> and <tt>--mojo</tt> should be removed.
14779
+ *
14780
+ * Example:
14781
+ *
14782
+ * - \File +t.rb+:
14783
+ *
14784
+ * # Print arguments (and options, if any) found on command line.
14785
+ * p ['ARGV', ARGV]
14786
+ *
14787
+ * - Command and output:
14788
+ *
14789
+ * $ ruby t.rb --xyzzy --mojo foo.txt bar.txt
14790
+ * ["ARGV", ["--xyzzy", "--mojo", "foo.txt", "bar.txt"]]
14791
+ *
14792
+ * \ARGF's stream access considers the elements of +ARGV+, left to right:
14793
+ *
14794
+ * - \File +t.rb+:
14795
+ *
14796
+ * p "ARGV: #{ARGV}"
14797
+ * p "Line: #{ARGF.read}" # Read everything from all specified streams.
14798
+ *
14799
+ * - Command and output:
14800
+ *
14801
+ * $ ruby t.rb foo.txt bar.txt
14802
+ * "ARGV: [\"foo.txt\", \"bar.txt\"]"
14803
+ * "Read: Foo 0\nFoo 1\nBar 0\nBar 1\nBar 2\nBar 3\n"
14804
+ *
14805
+ * Because the value at +ARGV+ is an ordinary array,
14806
+ * you can manipulate it to control which sources \ARGF considers:
14807
+ *
14808
+ * - If you remove an element from +ARGV+, \ARGF will not consider the corresponding source.
14809
+ * - If you add an element to +ARGV+, \ARGF will consider the corresponding source.
14810
+ *
14811
+ * Each element in +ARGV+ is removed when its corresponding source is accessed;
14812
+ * when all sources have been accessed, the array is empty:
14813
+ *
14814
+ * - \File +t.rb+:
14815
+ *
14816
+ * until ARGV.empty? && ARGF.eof?
14817
+ * p "ARGV: #{ARGV}"
14818
+ * p "Line: #{ARGF.readline}" # Read each line from each specified stream.
14819
+ * end
14820
+ *
14821
+ * - Command and output:
14822
+ *
14823
+ * $ ruby t.rb foo.txt bar.txt
14824
+ * "ARGV: [\"foo.txt\", \"bar.txt\"]"
14825
+ * "Line: Foo 0\n"
14826
+ * "ARGV: [\"bar.txt\"]"
14827
+ * "Line: Foo 1\n"
14828
+ * "ARGV: [\"bar.txt\"]"
14829
+ * "Line: Bar 0\n"
14830
+ * "ARGV: []"
14831
+ * "Line: Bar 1\n"
14832
+ * "ARGV: []"
14833
+ * "Line: Bar 2\n"
14834
+ * "ARGV: []"
14835
+ * "Line: Bar 3\n"
14836
+ *
14837
+ * ==== Filepaths in +ARGV+
14838
+ *
14839
+ * The +ARGV+ array may contain filepaths the specify sources for \ARGF reading.
14840
+ *
14841
+ * This program prints what it reads from files at the paths specified
14842
+ * on the command line:
14843
+ *
14844
+ * - \File +t.rb+:
14845
+ *
14846
+ * p ['ARGV', ARGV]
14847
+ * # Read and print all content from the specified sources.
14848
+ * p ['ARGF.read', ARGF.read]
14849
+ *
14850
+ * - Command and output:
14851
+ *
14852
+ * $ ruby t.rb foo.txt bar.txt
14853
+ * ["ARGV", [foo.txt, bar.txt]
14854
+ * ["ARGF.read", "Foo 0\nFoo 1\nBar 0\nBar 1\nBar 2\nBar 3\n"]
14855
+ *
14856
+ * ==== Specifying <tt>$stdin</tt> in +ARGV+
14857
+ *
14858
+ * To specify stream <tt>$stdin</tt> in +ARGV+, us the character <tt>'-'</tt>:
14859
+ *
14860
+ * - \File +t.rb+:
14861
+ *
14862
+ * p ['ARGV', ARGV]
14863
+ * p ['ARGF.read', ARGF.read]
14864
+ *
14865
+ * - Command and output:
14866
+ *
14867
+ * $ echo "Open the pod bay doors, Hal." | ruby t.rb -
14868
+ * ["ARGV", ["-"]]
14869
+ * ["ARGF.read", "Open the pod bay doors, Hal.\n"]
14870
+ *
14871
+ * When no character <tt>'-'</tt> is given, stream <tt>$stdin</tt> is ignored
14872
+ * (exception: see {Special Case}[rdoc-ref:ARGF@Special+Case]):
14873
+ *
14874
+ * - Command and output:
14875
+ *
14876
+ * $ echo "Open the pod bay doors, Hal." | ruby t.rb foo.txt bar.txt
14877
+ * "ARGV: [\"foo.txt\", \"bar.txt\"]"
14878
+ * "Read: Foo 0\nFoo 1\nBar 0\nBar 1\nBar 2\nBar 3\n"
14879
+ *
14880
+ * ==== Mixtures and Repetitions in +ARGV+
14881
+ *
14882
+ * For an \ARGF reader, +ARGV+ may contain any mixture of filepaths
14883
+ * and character <tt>'-'</tt>, including repetitions.
14884
+ *
14885
+ * ==== Modifications to +ARGV+
14886
+ *
14887
+ * The running Ruby program may make any modifications to the +ARGV+ array;
14888
+ * the current value of +ARGV+ affects \ARGF reading.
14889
+ *
14890
+ * ==== Empty +ARGV+
14891
+ *
14892
+ * For an empty +ARGV+, an \ARGF read method either returns +nil+
14893
+ * or raises an exception, depending on the specific method.
14894
+ *
14895
+ * === More Read Methods
14896
+ *
14897
+ * As seen above, method ARGF#read reads the content of all sources
14898
+ * into a single string.
14899
+ * Other \ARGF methods provide other ways to access that content;
14900
+ * these include:
14901
+ *
14902
+ * - Byte access: #each_byte, #getbyte, #readbyte.
14903
+ * - Character access: #each_char, #getc, #readchar.
14904
+ * - Codepoint access: #each_codepoint.
14905
+ * - Line access: #each_line, #gets, #readline, #readlines.
14906
+ * - Source access: #read, #read_nonblock, #readpartial.
14907
+ *
14908
+ * === About \Enumerable
14909
+ *
14910
+ * \ARGF includes module Enumerable.
14911
+ * Virtually all methods in \Enumerable call method <tt>#each</tt> in the including class.
14912
+ *
14913
+ * <b>Note well</b>: In \ARGF, method #each returns data from the _sources_,
14914
+ * _not_ from +ARGV+;
14915
+ * therefore, for example, <tt>ARGF#entries</tt> returns an array of lines from the sources,
14916
+ * not an array of the strings from +ARGV+:
14917
+ *
14918
+ * - \File +t.rb+:
14919
+ *
14920
+ * p ['ARGV', ARGV]
14921
+ * p ['ARGF.entries', ARGF.entries]
14922
+ *
14923
+ * - Command and output:
14924
+ *
14925
+ * $ ruby t.rb foo.txt bar.txt
14926
+ * ["ARGV", ["foo.txt", "bar.txt"]]
14927
+ * ["ARGF.entries", ["Foo 0\n", "Foo 1\n", "Bar 0\n", "Bar 1\n", "Bar 2\n", "Bar 3\n"]]
14928
+ *
14929
+ * == Writing
14930
+ *
14931
+ * If <i>inplace mode</i> is in effect,
14932
+ * \ARGF may write to target streams,
14933
+ * which at any particular time are determined by the content of ARGV.
14934
+ *
14935
+ * Methods about inplace mode:
14936
+ *
14937
+ * - #inplace_mode
14938
+ * - #inplace_mode=
14939
+ * - #to_write_io
14940
+ *
14941
+ * Methods for writing:
14942
+ *
14943
+ * - #print
14944
+ * - #printf
14945
+ * - #putc
14946
+ * - #puts
14947
+ * - #write
14740
14948
*
14741
- * $ echo Glark > file1
14742
- * $ echo "glark" | ruby -e 'p ARGF.read' -- - file1
14743
- * "glark\nGlark\n"
14744
14949
*/
14745
14950
14746
14951
/*
0 commit comments