I need to override require from within a Ruby file, which is required from my start.rb, which is the application entry point. rubygems is loaded before this, in start.rb.
Everything I tried gave me a stack overflow error.
What is the right way to do it?
Generally, if you want to patch some built-in method, you should first make an alias for the original method. Most of the time you'll call the old one somewhere in your overriding method. Otherwise you'll lost the functionality of the original method and it's likely to break the application logic.
ri require or read the documentation to find out where the require method is defined. You'll find it's in Kernel module. Besides, you'll find its method signature so you know what the parameter list looks like.Kernel. DON'T break the functionality unless you know what you are doing.module Kernel
  # make an alias of the original require
  alias_method :original_require, :require
  # rewrite require
  def require name
    puts name
    original_require name
  end
end
# test the new require
require 'date'
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With