Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Kernel#warn

warn(*msgs, uplevel: nil, category: nil) → nil

If warnings have been disabled (for example with the -W0 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 uplevel keyword argument is given, the string will be prepended with information for the given caller frame in the same format used by the rb_warn 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

If category keyword argument is given, passes the category to Warning.warn. 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.

See also #URI #y #untrace_var #yield_self also in 4.1 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0