2019-12-10 14:22:42 +03:00
|
|
|
# encoding: utf-8
|
2020-01-02 06:12:05 +03:00
|
|
|
# frozen-string-literal: true
|
2019-12-10 14:22:42 +03:00
|
|
|
|
|
|
|
module Kernel
|
2019-12-13 14:51:58 +03:00
|
|
|
module_function
|
2019-12-10 14:22:42 +03:00
|
|
|
|
|
|
|
# call-seq:
|
2020-12-08 13:32:33 +03:00
|
|
|
# warn(*msgs, uplevel: nil, category: nil) -> nil
|
2019-12-10 14:22:42 +03:00
|
|
|
#
|
|
|
|
# If warnings have been disabled (for example with the
|
|
|
|
# <code>-W0</code> flag), does nothing. Otherwise,
|
|
|
|
# converts each of the messages to strings, appends a newline
|
|
|
|
# character to the string if the string does not end in a newline,
|
|
|
|
# and calls Warning.warn with the string.
|
|
|
|
#
|
|
|
|
# warn("warning 1", "warning 2")
|
|
|
|
#
|
|
|
|
# <em>produces:</em>
|
|
|
|
#
|
|
|
|
# warning 1
|
|
|
|
# warning 2
|
|
|
|
#
|
|
|
|
# If the <code>uplevel</code> keyword argument is given, the string will
|
|
|
|
# be prepended with information for the given caller frame in
|
|
|
|
# the same format used by the <code>rb_warn</code> C function.
|
|
|
|
#
|
|
|
|
# # In baz.rb
|
|
|
|
# def foo
|
|
|
|
# warn("invalid call to foo", uplevel: 1)
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def bar
|
|
|
|
# foo
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# bar
|
|
|
|
#
|
|
|
|
# <em>produces:</em>
|
|
|
|
#
|
|
|
|
# baz.rb:6: warning: invalid call to foo
|
|
|
|
#
|
2020-09-28 20:10:31 +03:00
|
|
|
# If <code>category</code> keyword argument is given, passes the category
|
|
|
|
# to <code>Warning.warn</code>. The category given must be be one of the
|
|
|
|
# following categories:
|
|
|
|
#
|
|
|
|
# :deprecated :: Used for warning for deprecated functionality that may
|
|
|
|
# be removed in the future.
|
|
|
|
# :experimental :: Used for experimental features that may change in
|
|
|
|
# future releases.
|
Add rb_category_warn{,ing} for warning messages with categories
This adds the following C-API functions that can be used to emit
warnings with categories included:
```c
void rb_category_warn(const char *, const char*, ...)
void rb_category_warning(const char*, const char*, ...)
```
Internally in error.c, there is an rb_warn_category function
that will call Warning.warn with the string and the category
keyword if it doesn't have an arity of 1, and will call
Warning.warn with just the string if it has an arity of 1.
This refactors the rb_warn_deprecated{,_to_remove} functions
to use rb_warn_category.
This makes Kernel#warn accept a category keyword and pass it
to Warning.warn, so that Ruby methods can more easily emit
warnings with categories. rb_warn_category makes sure that
the passed category is a already defined category symbol
before calling Warning.warn.
The only currently defined warning category is :deprecated,
since that is what is already used. More categories can be
added in later commits.
2020-09-03 18:00:10 +03:00
|
|
|
def warn(*msgs, uplevel: nil, category: nil)
|
|
|
|
Primitive.rb_warn_m(msgs, uplevel, category)
|
2019-12-10 14:22:42 +03:00
|
|
|
end
|
|
|
|
end
|