On 2 Nov 2007, at 11:38, ara.t.howard wrote:

>
> On Nov 1, 2007, at 6:18 PM, Trans wrote:
>
>> Is there any way to ask a file what other file require/load 'd it? I
>> imagine this has been asked before but I couldnt seem to find good
>> terms to search for it.
>>
>
> cfp:~ > cat a.rb
> p Kernel.requiree('main')
>
> require 'b'
>
> p Kernel.requiree('main')
>
> BEGIN {
>  module Kernel
>    h = Hash.new
>    define_method(:requiree) do |of|
>      h[of]
>    end
>
>    r = method :require
>    define_method(:require) do |*a|
>      r.call *a
>      h[a.first] = caller
>    end
>  end
> }
>
>
> cfp:~ > cat b.rb
> require 'main'
>
>
> cfp:~ > ruby a.rb
> nil
> ["./b.rb:1", "a.rb:20:in `require'", "a.rb:20:in `call'", "a.rb: 
> 20:in `require'", "a.rb:4"]


Ara, out of curiosity, is there any reason why you don't write the  
Kernel modification code as:

module Kernel
   H = {}
   def requiree(of)
     H[of]
   end
	
   alias :old_require :require
   def require(*a)
     old_require(*a)
     H[a.first] = caller
   end
end

Is there any difference between the two versions, or is it just a  
style preference?

Alex Gutteridge

Bioinformatics Center
Kyoto University