Module: Warning
- Defined in:
- error.c,
error.c
Overview
The Warning module contains a single method named #warn, and the module extends itself, making Warning.warn available. Warning.warn is called for all warnings issued by Ruby. By default, warnings are printed to $stderr.
Changing the behavior of Warning.warn is useful to customize how warnings are handled by Ruby, for instance by filtering some warnings, and/or outputting warnings somewhere other than $stderr
.
If you want to change the behavior of Warning.warn you should use Warning.extend(MyNewModuleWithWarnMethod)
and you can use super
to get the default behavior of printing the warning to $stderr
.
Example:
module MyWarningFilter
def warn(, category: nil, **kwargs)
if /some warning I want to ignore/.match?()
# ignore
else
super
end
end
end
Warning.extend MyWarningFilter
You should never redefine Warning#warn (the instance method), as that will then no longer provide a way to use the default behavior.
The warning gem provides convenient ways to customize Warning.warn.
Defined Under Namespace
Classes: buffer
Class Method Summary collapse
-
.[](category) ⇒ Boolean
Returns the flag to show the warning messages for
category
. -
.[]=(category) ⇒ Object
Sets the warning flags for
category
. -
.categories ⇒ Array
Returns a list of the supported category symbols.
Instance Method Summary collapse
-
#warn(msg, category: nil) ⇒ nil
Writes warning message
msg
to $stderr.
Class Method Details
.[](category) ⇒ Boolean
Returns the flag to show the warning messages for category
. Supported categories are:
:deprecated
-
deprecation warnings
-
assignment of non-nil value to
$,
and$;
-
keyword arguments
etc.
-
:experimental
-
experimental features
:performance
-
performance hints
-
Shape variation limit
-
227 228 229 230 231 232 |
# File 'error.c', line 227
static VALUE
rb_warning_s_aref(VALUE mod, VALUE category)
{
rb_warning_category_t cat = rb_warning_category_from_name(category);
return RBOOL(rb_warning_category_enabled_p(cat));
}
|
.[]=(category) ⇒ Object
Sets the warning flags for category
. See Warning.[] for the categories.
242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'error.c', line 242
static VALUE
rb_warning_s_aset(VALUE mod, VALUE category, VALUE flag)
{
unsigned int mask = rb_warning_category_mask(category);
unsigned int disabled = warning_disabled_categories;
if (!RTEST(flag))
disabled |= mask;
else
disabled &= ~mask;
warning_disabled_categories = disabled;
return flag;
}
|
.categories ⇒ Array
Returns a list of the supported category symbols.
262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'error.c', line 262
static VALUE
rb_warning_s_categories(VALUE mod)
{
st_index_t num = warning_categories.id2enum->num_entries;
ID *ids = ALLOCA_N(ID, num);
num = st_keys(warning_categories.id2enum, ids, num);
VALUE ary = rb_ary_new_capa(num);
for (st_index_t i = 0; i < num; ++i) {
rb_ary_push(ary, ID2SYM(ids[i]));
}
return rb_ary_freeze(ary);
}
|
Instance Method Details
#warn(msg, category: nil) ⇒ nil
Writes warning message msg
to $stderr. This method is called by Ruby for all emitted warnings. A category
may be included with the warning.
See the documentation of the Warning module for how to customize this.
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'error.c', line 286
static VALUE
rb_warning_s_warn(int argc, VALUE *argv, VALUE mod)
{
VALUE str;
VALUE opt;
VALUE category = Qnil;
rb_scan_args(argc, argv, "1:", &str, &opt);
if (!NIL_P(opt)) rb_get_kwargs(opt, &id_category, 0, 1, &category);
Check_Type(str, T_STRING);
rb_must_asciicompat(str);
if (!NIL_P(category)) {
rb_warning_category_t cat = rb_warning_category_from_name(category);
if (!rb_warning_category_enabled_p(cat)) return Qnil;
}
rb_write_error_str(str);
return Qnil;
}
|