您能要求红宝石将警告视为错误吗? [英] Can you ask ruby to treat warnings as errors?

查看:152
本文介绍了您能要求红宝石将警告视为错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

红宝石允许您将警告视为错误吗?

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.

推荐答案

不幸的是,目前尚无真正的方法,至少目前在大多数Ruby版本中都没有(可能存在 变体), 缺少监视程序输出的功能,并在标准错误出现警告时中止该操作.原因如下:

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定义了 Kernel.warn ,您可以重新定义以执行您希望做的任何事情(包括退出),并且您希望 (希望)被Ruby一致地用于报告警告(包括内部解析警告),但是
  • Ruby内部的
  • 本机实现的方法(在C语言中)将依次直接从source/server.c调用一个名为rb_warn的本机方法,完全绕过对Kernel.warn的重新定义(例如,在执行类似do_something if 'string'之类的操作时发出的"string literal in condition"警告,是通过来自source/parse.c的本机rb_warn打印的)
  • 使情况更糟的是,还有另外一个rb_warning本机方法,如果指定了-w-v,Ruby可以使用该方法记录警告.
  • 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.

因此,如果您只需要对应用程序代码调用Kernel.warn生成的警告采取措施,则只需重新定义Kernel.warn.否则,您只有两种选择:

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. 更改source/error.c以退出rb_warnrb_warning(和rb_warn_m?),并重建Ruby
  2. 监视程序的标准错误输出": warning:",并在匹配时中止输出
  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

这篇关于您能要求红宝石将警告视为错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆