From: mame@...
Date: 2020-08-19T04:05:32+00:00
Subject: [ruby-core:99634] [Ruby master Feature#17122] Add category to	Warning#warn

Issue #17122 has been updated by mame (Yusuke Endoh).


Hi Eileen,

First of all, thank you not only for contributing to Ruby but also for working hard on keyword argument separation for GitHub code base!

I think that this feature is reasonable, but I concern its compatibility.

```
module Warning
  def self.warn(msg)
    p msg
  end
end

Object.new.tainted?
  #=> 2.7: "t.rb:9: warning: Object#tainted? is deprecated and will be removed in Ruby 3.2.\n"
  #=> master with your patch: wrong number of arguments (given 2, expected 1) (ArgumentError)
```

2.7 works great, but master with your PR passes extra argument, which causes an error.
Jeremy says that he can update gem ("warning" gem), but I guess that this kind of code is used in other places.
Do you think if such a code is less often so that we can ignore?


In addition: your PR changed only `rb_warn_deprecated_to_remove` but not `rb_warn_deprecated`.  Is there any reason?

----------------------------------------
Feature #17122: Add category to Warning#warn
https://bugs.ruby-lang.org/issues/17122#change-87115

* Author: eileencodes (Eileen Uchitelle)
* Status: Open
* Priority: Normal
----------------------------------------
Deprecation warnings and other warnings in Ruby have a category (:deprecated, etc) but those categories aren't exposed or accessible. In the most recent Ruby 2.7 upgrade at GitHub we monkey patched `Warning#warn` to be able to turn warnings into exceptions. However, there was no way to tell which warnings were deprecations and which were other types of warnings.

I want to expose the `category` on the `Warning` module so that I'm able to monkey patch `Warning#warn` and treat deprecation warnings differently from other warnings without using a regex the strings.

Here's an example program demonstrating what I'd like to get from Ruby by implementing this feature:

```ruby
module Warning
  def self.warn(msg, category: nil)
    if category == :deprecated
      raise msg 
    else
      super
    end 
  end 
end

def ivar
  Object.new.instance_variable_get(:@ivar)
end

# Doesn't raise, but warns with verbose set
ivar

# Raises an error
Object.new.tainted?
```

The PR I worked on with @tenderlove is here: https://github.com/ruby/ruby/pull/3418

It moves the `Warning` module to be written in Ruby, updates `rb_warning_s_warn` to pass kwargs, and adds a `category` to `Warning#warn`. 



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>