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
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.
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:
- 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