您能要求红宝石将警告视为错误吗? [英] Can you ask ruby to treat warnings as errors?
问题描述
红宝石允许您将警告视为错误吗?
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
fromsource/server.c
, completely bypassing your redefinition ofKernel.warn
(e.g. the "string literal in condition
" warning, for example, issued when doing something like:do_something if 'string'
, is printed via the nativerb_warn
fromsource/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:
- 更改
source/error.c
以退出rb_warn
和rb_warning
(和rb_warn_m
?),并重建Ruby - 监视程序的标准错误输出"
: warning:
",并在匹配时中止输出
- alter
source/error.c
to exit inrb_warn
andrb_warning
(andrb_warn_m
?), and rebuild Ruby - monitor your program's standard error output for '
: warning:
', and abort it on match
这篇关于您能要求红宝石将警告视为错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!