Written by someone called 'erikh' on #rubyonrails on irc.freenode.net.
1 module Which
2
3
4
5
6
7
8
9
10
11
12
13
14 def which(filename, path=ENV["PATH"], path_sep=File::PATH_SEPARATOR, dir_sep=File::SEPARATOR)
15 dirs = path.split(/#{path_sep}/)
16
17 locations = []
18
19 dirs.each do |dir|
20 newfile = "#{dir}#{dir_sep}#{filename}"
21
22 newfile.gsub!(/#{dir_sep}{2,}/, "#{dir_sep}")
23 p = Pathname.new(newfile)
24 if p.exist? and p.executable?
25 locations.push(newfile)
26 end
27 end
28
29 return locations
30 end
31
32 module_function :which
33 end