Hacker News new | ask | show | jobs
by fidz 3460 days ago
Sure.

    require 'pry'
    module Kernel
      def debug; binding.pry; end
    end
    
    x = "hi"
    debug
2 comments

Won't the binding then be your monkey patched debug method on kernel instead of where you called it from?
Yes.

    irb(main):008:0> x = "1"
    => "1"
    irb(main):009:0> debug
    [1] pry(main)> x
    NameError: undefined local variable or method `x' for main:Object
    from (pry):1:in `debug'
If you really want to do this, you can use binding_of_caller[1] to create binding object (`Kernel.binding`) up in the call stack:

    require 'binding_of_caller'
    require 'pry'
    
    module Kernel
      def debug
        binding.of_caller(1).pry
      end
    end
Then:

    irb(main):008:0> x = "1"
    => "1"
    irb(main):009:0> debug
    [1] pry(main)> x
    => "1"
[1]: https://github.com/banister/binding_of_caller
I was imagining more at the level of the standard library or the pry library.

Or do you recommend this monkeypatch in practice?