From: Erik Veenstra Date: 2006-01-01T06:57:55+09:00 Subject: Re: my stupid code ... > I'm trying to write a small ruby script which accepts: > > 1. Input : File or $stdin > 2. Output : File or $stdout Abstraction is the keyword. Put the decision to open a file or stdin or stdout in a a method on the class File and put that code in a library. That keeps your application clean and simple. gegroet, Erik V. - http://www.erikveen.dds.nl/ ---------------------------------------------------------------- # LIBRARY class File def self.open_std(file, mode="r", *rest, &block) if file.nil? if block_given? block.call(mode.include?("r") ? $stdin : $stdout) else mode.include?("r") ? $stdin : $stdout end else File.open(file, mode, *rest, &block) end end end ---------------------------------------------------------------- # APPLICATION require "your_library" def do_some_thing(str) # do some things over str str.upcase end File.open_std(ARGV.shift, "r") do |f_in| File.open_std(ARGV.shift, "w") do |f_out| f_in.each do |line| f_out.puts do_some_thing(line) end end end ----------------------------------------------------------------