On 8/29/08, Matthew Moss <matthew.moss / gmail.com> wrote:
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>
> The three rules of Ruby Quiz 2:
>
> What I would like is a script that works like `which` but for Ruby
> modules. Examples:
>
Here's my solution.  I used the mod_req_level counter to restrict the
output to the files required at the top level.  Without it, requiring
rubygems prints the rubygems location about 9 times.
-Adam

alias :original_require :require

$mod_req_level=0
$mod_req_exts = %w{rb rbw o so dll bundle sl jar}.map{|ex| Regexp.new
"\.#{ex}$"}

def require lib
    $:.each{|d| files=Dir.glob(d+"/#{lib}*")
      f=files.find{|f|$mod_req_exts.find{|re|re=~f}}
      (puts "require #{lib} => #{f}";break) if f
    } if 1==($mod_req_level+=1)
  begin
    original_require lib
  ensure
    $mod_req_level-=1
  end
end

if __FILE__==$0
  ARGV.each{|f| require f}
end