Class: File
- Inherits:
-
Object
- Object
- File
- Defined in:
- lib/upm/core_ext.rb
Class Method Summary collapse
-
.which(*bins) ⇒ Object
Overly clever which(), which returns an array if more than one argument was supplied, or string/nil if only one argument was supplied.
- .which_is_best?(*bins) ⇒ Boolean
Class Method Details
.which(*bins) ⇒ Object
Overly clever which(), which returns an array if more than one argument was supplied, or string/nil if only one argument was supplied.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/upm/core_ext.rb', line 13 def self.which(*bins) results = [] bins = bins.flatten paths = ENV["PATH"].split(":").map { |path| File.realpath(path) rescue nil }.compact.uniq paths.each do |dir| bins.each do |bin| full_path = File.join(dir, bin) if File.exists?(full_path) if bins.size == 1 return full_path else results << full_path end end end end bins.size == 1 ? nil : results end |
.which_is_best?(*bins) ⇒ Boolean
37 38 39 40 |
# File 'lib/upm/core_ext.rb', line 37 def self.which_is_best?(*bins) result = which(*bins.flatten) result.is_a?(Array) ? result.first : result end |