[#3986] Re: Principle of least effort -- another Ruby virtue. — Andrew Hunt <andy@...>

> Principle of Least Effort.

14 messages 2000/07/14

[#4043] What are you using Ruby for? — Dave Thomas <Dave@...>

16 messages 2000/07/16

[#4139] Facilitating Ruby self-propagation with the rig-it autopolymorph application. — Conrad Schneiker <schneik@...>

Hi,

11 messages 2000/07/20

[ruby-talk:04221] Re: getopt(s|long) and ARGF

From: Dave Thomas <Dave@...>
Date: 2000-07-26 16:31:46 UTC
List: ruby-talk #4221
Hugh Sasse Staff Elec Eng <hgs@dmu.ac.uk> writes:

> How do the getopt libraries interact with ARGF?  I know they can remove
> things from ARGV, but will this mean that ARGF is affected at the same
> time?  I'm not sure from the information I have found.

ARGF used ARGV whenever it needs to: it is evaluated lazily. What this 
means is that whenever you do a gets, ARGF says "do I have a file to
read from?" If not, it picks the next thing off ARGV and uses it. For
example, here's som code that skips a file in the middle of ARGV.


     File.open("_f1", "w") {|f| f.puts "f1" }
     File.open("_f2", "w") {|f| f.puts "f2" }

     ARGV.shift while ARGV.length > 0

     ARGV <<  "_f1" << "gumby" << "_f2" 

     puts gets
     ARGV.shift   # skip gumby
     puts gets

And here it is again, changing the name of the next fil to open just
before it opens it.

     File.open("_f1", "w") {|f| f.puts "f1" }
     File.open("_f2", "w") {|f| f.puts "f2" }

     ARGV.shift while ARGV.length > 0

     ARGV <<  "_f1" << "gumby"

     puts gets
     ARGV.shift   # skip gumby
     ARGV << "_f2"
     puts gets


Hope this helps.


Dave

In This Thread