Class: Which

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-which.rb

Overview

<manveru> you need organization…

Class Method Summary collapse

Class Method Details

.which(lib) ⇒ Object Also known as: require

Example usage:

puts Which.which( 'rails' )

Or from your shell:

rwhich rails

Returns the full library path if the given library is found in the $LOAD_PATH. Under rare circumstances, an Array may be returned in the case of multiple matches under one path.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ruby-which.rb', line 19

def self.which( lib )
  begin
    Kernel#require( lib )
  rescue LoadError
    return nil
  end

  $LOAD_PATH.each do |path|
    extension = nil
    if File.extname( lib ).empty?
      extension = '.{rb,so,o,dll,class}'
    end
    found = Dir[ "#{path}/#{lib}#{extension}" ]
    if found.size == 1
      return found[ 0 ]
    elsif found.any?
      return found
    end
  end

  nil
end