你能要求 ruby​​ 将警告视为错误吗?

发布于 2024-07-15 02:49:03 字数 80 浏览 4 评论 0原文

ruby 是否允许您将警告视为错误?

我这样做的一个原因是为了确保如果删除一行代码意味着出现警告,我可以选择确保突变体被杀死。

Does ruby allow you to treat warnings as errors?

One reason I'd like to do this is to ensure that if heckle removing a line of code means that a warning occurs, I have the option of ensuring that the mutant get killed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

下雨或天晴 2024-07-22 02:49:03

不幸的是,没有真正的方法可以做到这一点,至少在大多数版本的 Ruby 上都没有(可能存在变体),缺乏监视程序输出并在以下情况下中止它:标准错误上会出现警告。 原因如下:

  • Ruby 定义了 Kernel.warn,您可以重新定义以执行您想要的任何操作(包括退出),并且您期望(希望)使用它Ruby 一致地报告警告(包括内部,例如解析警告),但是
  • Ruby 内部本地实现的方法(用 C 语言)将依次直接调用名为 的本机方法rb_warn 来自 source/server.c,完全绕过您对 Kernel.warn 的重新定义(例如“字符串文字in condition”警告,例如,在执行以下操作时发出:do_something if 'string',通过来自 source/ 的本机 rb_warn 打印parse.c
  • 让事情变得更糟,还有一个额外的 rb_warning 本机方法,Ruby 可以使用它来记录警告,如果 -w 或指定了-v

因此,如果您只需要对应用程序代码调用 Kernel.warn 生成的警告采取行动,那么只需重新定义 Kernel.warn 即可。 否则,您只有两个选择

  1. 更改 source/error.c 以在 rb_warnrb_warning(以及 rb_warn_m?),并重建 Ruby
  2. 监视程序的标准错误输出“:警告:”,并在匹配时中止它

There is unfortunately no real way of doing this, at least not on most versions of Ruby out there (variations may exist), short of monitoring the program output and aborting it when a warning appears on standard error. Here's why:

  • Ruby defines Kernel.warn, which you can redefine to do whatever you wish (including exiting), and which you'd expect (hope) to be used consistently by Ruby to report warnings (including internal e.g. parsing warning), but
  • methods implemented natively (in C) inside Ruby will in turn directly invoke a native method called rb_warn from source/server.c, completely bypassing your redefinition of Kernel.warn (e.g. the "string literal in condition" warning, for example, issued when doing something like: do_something if 'string', is printed via the native rb_warn from source/parse.c)
  • to make things even worse, there is an additional, rb_warning native method, which can be used by Ruby to log warnings if -w or -v is specified.

So, if you need to take action solely on warnings generated by your application code's calling Kernel.warn then simply redefine Kernel.warn. Otherwise, you have exactly two options:

  1. alter source/error.c to exit in rb_warn and rb_warning (and rb_warn_m?), and rebuild Ruby
  2. monitor your program's standard error output for ': warning:', and abort it on match
染柒℉ 2024-07-22 02:49:03

您最终可以通过重写 Warning.warn 来做到这一点,如下所示

module Warning
  def warn(msg)
    raise msg
  end
end

,这会将警告转变为异常。 该解决方案至少从 2.4 分支开始有效。

You can finally do that by overriding Warning.warn like

module Warning
  def warn(msg)
    raise msg
  end
end

This will turn the warning into an exception. This solution works at least since 2.4 branch.

执妄 2024-07-22 02:49:03

您还可以使用 DTrace,并拦截对 rb_warnrb_warning 的调用,尽管这不会产生您可以从某处挽救的异常。 相反,它只会将它们放在您可以轻松记录它们的地方。

You could also potentially use DTrace, and intercept the calls to rb_warn and rb_warning, though that's not going to produce exceptions you can rescue from somewhere. Rather, it'll just put them somewhere you can easily log them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文