[ruby-core:76045] [Ruby trunk Feature#12482] ArgumentError.new(nil) should give a better description
From:
duerst@...
Date:
2016-06-16 04:18:30 UTC
List:
ruby-core #76045
Issue #12482 has been updated by Martin D端rst.
Eike Dierks wrote:
> OK, that change would break the spec.
> I do understand that breaking the spec something that we rarely want to do.
It's not just that it formally breaks the spec. The documentation says that Exceptions take a *message* as an argument. This essentially means that it's the caller's responsibility to make sure the output is understandable.
Nil is not a message. Ruby is so kind as to stringify 'messages' that are not strings. Stringification of *nil* is "", so that is what you end up with.
> I found a workaround:
> ArgumentError.new([arg]) # wrap arg in Array to give a better error message when arg is nil
> works for me.
What about ArgumentError.new("nil") ? That would be clearer. But in general, even that isn't very helpful, something like ArgumentError.new("Got nil, but expected ...") would be much more helpful.
----------------------------------------
Feature #12482: ArgumentError.new(nil) should give a better description
https://bugs.ruby-lang.org/issues/12482#change-59248
* Author: Eike Dierks
* Status: Open
* Priority: Normal
* Assignee:
----------------------------------------
Currently:
~~~ruby
ArgumentError.new(nil) => #<ArgumentError: ArgumentError>
ArgumentError.new(nil).to_s => "ArgumentError"
ArgumentError.new(nil).inspect => "#<ArgumentError: ArgumentError>"
~~~
I want to suggest to change this to "ArgumentError(nil)" instead.
Rational:
For testing arguments, I frequently use:
~~~ruby
raise ArgumentError.new(arg) unless ExpectedClass === arg
~~~
However when arg is nil, this raises "ArgumentError" while raising "ArgumentError(nil)" would be much nicer.
This would differentiate between `ArgumentError.new()` and `ArgumentError.new(nil)`
Suggested changes
It looks like `ArgumentError#initialize` inherits Exception#initialize
I'd like to suggest to differentiate between Exception.new() and Exception.new(nil)
~~~ruby
def Exception.new(msg = :__Exception_msg_arg_undefined___)
case arg
when :__Exception_msg_arg_undefined___
# msg = "ClassName"
when nil
# msg = "ClassName(nil)"
end
~~~
Impact
I believe this should not break existing code as no code should rely on the string returned.
--
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>