Howto replace custom_require functionality

If I try a modification to custom_require and just run the code (in
netbeans) the paths are not listed, whereas if debugging, the paths are
listed.

a) What is the program flow in jruby that causes this?

b) IF it is possible to override (monkey patch) custom require, how
would it need to be done? I have tried monkey patching the kernel but
my attempt did not work.

def require(path) # :doc:
puts path # <<<< trivial addition
gem_original_require path
rescue LoadError => load_error
if load_error.message =~ /#{Regexp.escape path}\z/ and
spec = Gem.searcher.find(path) then
Gem.activate(spec.name, “= #{spec.version}”)
gem_original_require path
else
raise load_error
end
end

Regards
Paul F Fraser


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Paul F Fraser wrote:

If I try a modification to custom_require and just run the code (in
netbeans) the paths are not listed, whereas if debugging, the paths
are listed.
This seems to be related to requiring rubygems. It seems the debugger
requires rubygems automatically, whereas my code had not required
rubygems.

 Gem.activate(spec.name, "= #{spec.version}")
 gem_original_require path

else
raise load_error
end
end
Any comments on point b) would be appreciated.

Thanks
Paul F Fraser


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

On Tue, Nov 24, 2009 at 10:18 PM, Paul F Fraser [email protected]
wrote:

b) IF it is possible to override (monkey patch) custom require, how would
it need to be done? I have tried monkey patching the kernel but my attempt
did not work.

I had to do this once in order to run functional tests against
rubygems itself. Here’s how:

  # point "require" back at the standard Ruby "require" method
  # if RubyGems has already aliased gem_original_require to the

original require…
if Kernel.private_method_defined?(:gem_original_require)
# get an unbound method reference to the original require method
unbound_gem_original_require = Kernel.send(:instance_method,
:gem_original_require)
# then remove the overridden RubyGems require
Kernel.send(:remove_method, :require)
# and define require to invoke the original require method
Kernel.send(:define_method, :require) {|path|
unbound_gem_original_require.bind(Kernel).call(path) }
end


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Chad W. wrote:

geminstaller/spec/helper/test_gem_home.rb at ec3c08fd9935e94dc96177923ca87b6d8a3105ce · thewoolleyman/geminstaller · GitHub
# and define require to invoke the original require method
Kernel.send(:define_method, :require) {|path|
unbound_gem_original_require.bind(Kernel).call(path) }
end
Thanks, Chad,
Some very clever code :slight_smile: . Don’t think I would have figured it out…
Regards
Paul