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

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

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

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

···

On Fri, 9 Sep 2005, Dan Bikle wrote:

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

--

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

===============================================================================

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