From: Sean O'Halpin Date: 2007-11-03T09:14:46+09:00 Subject: Re: Who required that!? On 11/2/07, Sean O'Halpin wrote: > On 11/2/07, Joel VanderWerf wrote: > > Sean O'Halpin wrote: > > > > > Using alias is just a little too fragile. You could always generate a > > > unique alias name (using GUID or something) which would avoid the > > > problem, but grabbing a reference to the existing method and using > > > that is sure to work. > > > > Can you do the latter without breaking gem's require? > > > > -- > > vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407 > > > > > Yes, that's the point. If you redefine require by chaining to the > previous definition (rather than aliasing) no other part of the system > will know the difference. But the proof is in the pudding :) > > > require 'rubygems' > > 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 > } > > require 'main' > > p Kernel.requiree("main") > > Main { > def run > puts "Made it!" > end > } > > __END__ > $ ruby requiree.rb > ["/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:32:in > `gem_original_require'", > "/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:32:in > `require'", "requiree.rb:19"] > Made it! > > Regards, > Sean > > Scrub that - Joel you're right - it depends on load order. If you use ruby -rubygems ... this won't work because requiree is grabbing a reference to the original require method and so bypassing the rubygems redefinition. It works ok if rubygems aliases requiree's require but not the other way round. Hmmm... Regards, Sean