From: "Andreas S." Date: 2006-01-01T07:06:06+09:00 Subject: Re: my stupid code ... unknown wrote: > Hi, > I'm trying to write a small ruby script which accepts: > 1. Input : File or $stdin > 2. Output : File or $stdout > > This is my stupid code: > #--------------------------------------------------------- > def do_some_thing(str) > # do some things over str > end > > f_in = ARGV[0] > f_out = ARGV[1] > if f_out==nil > fout = File.open(f_out,"w") > end > > if f_in==nil > $stdin.each { |line| > if f_out==nil > print do_some_thing(line) > else > fout.print do_some_thing(line) > end > } > else > File.open(f_in,"r").each {|line| > if f_out==nil > print do_some_thing(line) > else > fout.print do_some_thing(line) > end > } > end > fout.close if f_out!=nil fin = ARGV[0] ? File.open(ARGV[0],'r') : $stdin fout = ARGV[1] ? File.open(ARGV[1], 'w') : $stdin fin.each_line do |line| fout.print do_some_thing(line) end Or (hides File.open errors): fin = File.open(ARGV[0],'r') rescue $stdin fout = File.open(ARGV[1], 'w') rescue $stdout fin.each_line do |line| fout.print do_some_thing(line) end -- Posted via http://www.ruby-forum.com/.