Ruby's module search path

I use:

def which(libname)
$:.each { |d|
f = File.join(d, libname)
p f if File.exists? f
f += ".rb"
p f if File.exists? f
}
nil
end

···

-----Original Message-----
From: Harry Ohlsen [mailto:harryo@qiqsolutions.com]
Sent: Wednesday, 24 March 2004 1:33 PM
To: ruby-talk ML
Subject: Ruby’s module search path

Is there an easy way to discover the path to a “require”-ed module
(assuming it’s a .rb file)?

Ie, is there some way to find out where the interpreter would find
fred.rb if I said

require ‘fred’

TIA

Harry O.

Mehr, Assaph (Assaph) wrote:

I use:

def which(libname)
$:.each { |d|
f = File.join(d, libname)
p f if File.exists? f
f += “.rb”
p f if File.exists? f
}
nil
end

Excellent! Works a treat. Thanks.

I just find it’s often handy to take a peek at the code for a module, but just slightly painful to locate the right file.

Cheers,

Harry O.