문제

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?

도움이 되었습니까?

해결책

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!"
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top