【问题标题】:Is there a way to autoexecute code when Pry is triggered?有没有办法在 Prey 被触发时自动执行代码?
【发布时间】:2014-04-22 00:30:56
【问题描述】:

有没有办法在遇到binding.pry 时自动执行代码?

例如,如果我想在每次调用binding.pry 时执行puts "Hey, I'm debugging!"?

【问题讨论】:

  • 我认为 pry 中没有类似的东西。你为什么要这么做?
  • Mike H-R 回答了这个问题。我想在触发调试器时使用它来触发对日志记录功能的更改,以便应用程序可以在不需要 CLI 标志的情况下进行上下文切换。

标签: ruby debugging pry


【解决方案1】:

我想看看代码并发现一些可能有用的东西,你要么必须将它添加到 gemfile 中,要么你可以将它添加到你的 .pryrc 中(我不知道这是否是否从binding.pry 调用)。从github似乎有:

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

来自the pry github hooks file。

编辑:这是一个注册钩子并执行它的示例(即初始化您的应用程序)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!"

【讨论】:

  • 顺便说一句,它可以直接进入你的应用程序初始化文件,而不是.pryrc。真的很有用!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-12
  • 1970-01-01
  • 1970-01-01
  • 2012-03-29
  • 2018-07-30
  • 1970-01-01
相关资源
最近更新 更多