whichコマンドをWindowsに

30分プログラム、その133。whichコマンドをさくっと再実装。

Windowsでwhichがつかえないのが嫌だったので実装してみた。でも、よく考えたらWindowsRubyは一般的じゃないっ!しかも、手元にWindowsマシンがないから試せないっ。

使い方

$ ruby which.rb ruby
/opt/local/bin/ruby

$ ruby which.rb ls
/bin/ls

ソースコード

#! /opt/local/bin/ruby -w
# -*- mode:ruby; coding:utf-8 -*-
#
# which.rb -
#
# Copyright(C) 2007 by mzp
# Author: MIZUNO Hiroki <hiroki1124@gmail.com> 
# http://mzp.sakura.ne.jp/
#
# Timestamp: 2007/09/13 18:27:38
#
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Ruby itself.
#
isWin = RUBY_PLATFORM =~ /mswin|mingw|cygwin|bccwin|interx/

cmd = ARGV.first
paths = ENV['PATH'].split(isWin ? ';' : ':').map{|path|
  File.expand_path cmd,path
}

puts paths.find{|path|
  File.exist?(path)
}