#!ruby -Ks class Which OPT_ALL = "-A" # コンストラクタ def initialize(param) @option = nil @filename = nil @path = nil if param.is_a?(Array) @option = param.include?(OPT_ALL) @filename = get_filename(param) @path = ENV['PATH'] end end # 探すファイル名を取り出す def get_filename(param) param.each do |x| return x if x != OPT_ALL end return nil end private :get_filename # ファイルを探して標準出力する def search() if @filename == nil print "Usage: which [-a]\n" else result = search_command() if result == "" print "command " + @filename + " not found.\n" else print result end end end # ファイルを探す本当の処理 def search_command() count = 0 result = "" @path.split(";").each do |path| path = path.gsub(/\A\"|\"\Z/,"") if FileTest.directory?(path) Dir.foreach(path) do |file| if FileTest.file?(path + "\\" + file) && file.match(/\A#{@filename}\./i) result += path + "\\" + file + "\n" count += 1 if !@option return result end end end end end return result end private :search_command end a = Which.new(ARGV.map{|x| x.upcase}) a.search