1

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?

2
  • i don't think that there is something like that built into pry. why do you want to do that? Commented Apr 22, 2014 at 1:53
  • Mike H-R answered it. I want to use it to trigger changes to logging features when a debugger is triggered so the application can context-switch without requiring CLI flags. Commented Apr 28, 2014 at 18:51

1 Answer 1

3

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!"
Sign up to request clarification or add additional context in comments.

1 Comment

BTW, it can go directly in your application init file, rather than in .pryrc. Really useful!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.