Question

Is there a way to automatically execute code when binding.pry is encountered?

For example, if I want to execute puts "Hey, I'm debugging!" every time binding.pry is called?

Was it helpful?

Solution

I felt like having a look at the code and found something that might be useful, you either have to add it to the gemfile or you might be able to add it to your .pryrc (I don't know if this gets called from binding.pry or not). From github there appears to be:

# @example Adding a hook for the `:before_session` event.
Pry.config.hooks.add_hook(:before_session, :say_hi) do
  puts "hello"
end

From the pry github hooks file.

EDIT: Here's an example to register the hook and execute it (i.e. init your app) from another part of the pry github hooks file:**

my_hook = Pry::Hooks.new.add_hook(:before_session, :say_hi) { puts "hi!" }
my_hook.exec_hook(:before_session) #=> OUTPUT: "hi!"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top