P
Paul Emmons
I'm experimenting with writing a text filter, as a learning
experience.
Right now, I want the program to:
* work as a pure filter if there are no command-line arguments
* use the argument as an input file, send output to STOUT if there is
one argument
* If there are more than one argument, use the last argument as
an output file name and the others as input file names.
My question is not how to get my program to do this. It is doing it
exactly as I wanted, like a mind reader. My question is, how is it so
smart? I expect I'll have problems later on if I don't really
understand what is happening.
I have read that unless somehow told otherwise, a Ruby program uses
the command-line arguments as input file names and reads them one
after the other as though they came from STDIN. If I have used the
last argument as an output file name, why doesn't it run into trouble
when it comes to this argument and tries to use it as in input file?
And how, in general, do I make sure that a Ruby script does not try to
use an argument as an input file name if I want it to mean something
else?
Scratching my head (with delight for a change).
Thank you for any help.
(This is my program so far.
#!/usr/bin/env ruby
# Reverses characters in every line of a file
if STDIN.tty?
puts "No. of args = #{ARGV.size}"
ARGV.each { |a| puts "Arg = #{a}" }
if ARGV.size>1
$stdout=File.new(ARGV[ARGV.size-1], "w")
end
end
while line = gets
line.chomp!
puts line.reverse
end
experience.
Right now, I want the program to:
* work as a pure filter if there are no command-line arguments
* use the argument as an input file, send output to STOUT if there is
one argument
* If there are more than one argument, use the last argument as
an output file name and the others as input file names.
My question is not how to get my program to do this. It is doing it
exactly as I wanted, like a mind reader. My question is, how is it so
smart? I expect I'll have problems later on if I don't really
understand what is happening.
I have read that unless somehow told otherwise, a Ruby program uses
the command-line arguments as input file names and reads them one
after the other as though they came from STDIN. If I have used the
last argument as an output file name, why doesn't it run into trouble
when it comes to this argument and tries to use it as in input file?
And how, in general, do I make sure that a Ruby script does not try to
use an argument as an input file name if I want it to mean something
else?
Scratching my head (with delight for a change).
Thank you for any help.
(This is my program so far.
#!/usr/bin/env ruby
# Reverses characters in every line of a file
if STDIN.tty?
puts "No. of args = #{ARGV.size}"
ARGV.each { |a| puts "Arg = #{a}" }
if ARGV.size>1
$stdout=File.new(ARGV[ARGV.size-1], "w")
end
end
while line = gets
line.chomp!
puts line.reverse
end