'which' command for Ruby (like csh which?)

D

Dan Bikle

Hi,

suppose I have,

require "redgreenblue"
in=20
runme.rb

Is there a way I can tell which=20
redgreenblue.rb file will get pulled in to runme.rb
at run time?

Most UNIX shells have a 'which' builtin command which=20
offers similar behavior.

If I type=20
which runme
the builtin which command will tell which runme file
the shell would run should I choose to run it.

Thanks.

-Dan
 
D

Dave Burt

Dan Bikle asked:
suppose I have,

require "redgreenblue"
in
runme.rb

Is there a way I can tell which
redgreenblue.rb file will get pulled in to runme.rb
at run time?

I don't know of anything built in, but this should do the trick:

def which(lib)
lib += ".rb" unless lib[-3..-1] == ".rb"
$LOAD_PATH.each do |path|
file = File.join(path, lib)
return file if FileTest.exist?(file)
end
if defined? Gem
@gempath_searcher ||= Gem::GemPathSearcher.new
if spec = @gempath_searcher.find(lib) and lib = spec.autorequire
lib += ".rb" unless lib[-3..-1] == ".rb"
spec.require_paths.each do |path|
file = File.join(spec.full_gem_path, path, lib)
return file if FileTest.exist?(file)
end
end
end
nil
end

Cheers,
Dave
 
A

Ara.T.Howard

Hi,

suppose I have,

require "redgreenblue"
in
runme.rb

Is there a way I can tell which
redgreenblue.rb file will get pulled in to runme.rb
at run time?

Most UNIX shells have a 'which' builtin command which
offers similar behavior.

If I type
which runme
the builtin which command will tell which runme file
the shell would run should I choose to run it.

Thanks.

-Dan

no. but not hard to write:

jib:~ > cat a.rb
require 'rbconfig'
require 'pathname'

def which_lib lib, opts = {}
lib = Pathname::new lib
search_path = opts['search_path'] || opts[:search_path] || $LOAD_PATH

dl_ext = "." << Config::CONFIG["DLEXT"]
rb_ext = "." << "rb"
pat = %r/#{ Regexp::escape dl_ext }$ | #{ Regexp::escape rb_ext }$/x

glob_for = lambda do |stem|
glob = "%s{,%s,%s}" % [stem, dl_ext, rb_ext]
Dir[glob].each do |path|
throw 'which', path if pat.match(path) and test(?r, path)
end
end

File::expand_path(
catch('which') do
if lib.absolute?
glob_for[ lib ]
else
search_path.each{|dir| glob_for[ File::join(dir, lib) ]}
end
return nil
end
)
end

p which_lib('session')
p which_lib('pty.so')
p which_lib(File::expand_path(__FILE__))
p which_lib('fubar')


jib:~ > ruby a.rb
"/dmsp/reference/ruby-1.8.1/lib/ruby/site_ruby/session.rb"
"/dmsp/reference/ruby-1.8.1/lib/ruby/1.8/i686-linux/pty.so"
"/home/ahoward/a.rb"
nil


should work on windows - but untested.

cheers.

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| Your life dwells amoung the causes of death
| Like a lamp standing in a strong breeze. --Nagarjuna
===============================================================================
 
G

gsinclair

Distributing an 'rwhich' command with Ruby would be quite handy,
methinks. There's a 'gemwhich' program with RubyGems:

$ gemwhich.cmd commandline
e:/ruby/lib/ruby/gems/1.8/gems/CommandLine-0.7.1/lib/commandline.rb

But strangely...

$ gemwhich.cmd commandline.rb
Can't find commandline.rb

Bug?

Cheers,
Gavin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top