Feature #19588
closedAllow Comparable#clamp(min, max) to accept nil as a specification
Description
Comparable#clamp(min, max)
(with two arguments) accepts nil
. This behaves the same as beginless/endless Range.
5.clamp(nil, 0) # => 0
5.clamp(10, nil) # => 10
5.clamp(..0) # => 0
5.clamp(10..) # => 10
This behavior is not documented. Presumably, this was not introduced intentionally.
The old Rubies did not accept accept a Range
argument.
In Ruby 2.7, accepting Range
as an argument was introduced.
At that time, the approach of passing nil
as a two-argument method was also discussed but not adopted,
and using Range was chosen instead. https://bugs.ruby-lang.org/issues/14784
However, in Ruby 3.0, the behavior of clamp
has changed to accept nil
.
This change is not documented in the NEWS or the documentation for clamp
,
and I believe that it was not an intentional change.
% docker run -it --rm rubylang/all-ruby env ALL_RUBY_SINCE=ruby-2.4.0 ./all-ruby -e "p 5.clamp(0, nil)"
ruby-2.4.0 -e:1:in `clamp': comparison of Integer with nil failed (ArgumentError)
from -e:1:in `<main>'
exit 1
...
ruby-2.7.8 -e:1:in `clamp': comparison of Integer with nil failed (ArgumentError)
from -e:1:in `<main>'
exit 1
ruby-3.0.0-preview1 5
...
ruby-3.2.2 5
It seems that https://github.com/ruby/ruby/commit/a93da4970be44a473b7b42e7516eb2663dece2c3 brought about this change.
How about making the current behavior a specification?
It has been three years since the behavior changed, and I don't see much point in prohibiting nil
now.
https://github.com/ruby/ruby/pull/7692 updates the documentation.
Updated by sawa (Tsuyoshi Sawada) about 2 years ago
I think it is a good idea. It should be synchronized with Range#cover?
.
Updated by kyanagi (Kouhei Yanagita) about 2 years ago
- Description updated (diff)
Updated by mame (Yusuke Endoh) about 2 years ago
Discussed at the dev meeting. @matz (Yukihiro Matsumoto) confirmed the behavior as is, so the proposed documentation change is accepted.
Updated by kyanagi (Kouhei Yanagita) about 2 years ago
The Japanese manual is updated.
https://docs.ruby-lang.org/ja/latest/class/Comparable.html#I_CLAMP
https://github.com/rurema/doctree/pull/2801
Updated by kyanagi (Kouhei Yanagita) over 1 year ago
If possible, could you merge the document and test update?
https://github.com/ruby/ruby/pull/7759
https://github.com/ruby/ruby/pull/7692
Updated by mame (Yusuke Endoh) over 1 year ago
- Status changed from Open to Closed
The two PRs are merged. Closing