From: "Ara.T.Howard" Date: 2005-09-10T00:39:17+09:00 Subject: Re: 'which' command for Ruby (like csh which?) 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 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 ===============================================================================