From: mame@... Date: 2021-02-17T17:36:11+00:00 Subject: [ruby-core:102563] [Ruby master Misc#17637] Endless ranges with `nil` boundary weird behavior Issue #17637 has been updated by mame (Yusuke Endoh). Hi, I proposed and implemented a endless range. This is a trade-off between early failure and usability/consistency. While the feature is indeed error-prone in some cases, it is more consistent and useful. It is possible to allow only `(1..)` and deny `(1..nil)`. In fact, `(1..nil)` used to raise an exception for a short period of development phase of Ruby 2.6. https://github.com/ruby/ruby/commit/48de2ea5f9b9067779acb0f7f76e5f879f2b42c0 But, to create a conditionally endless range, we need to write `max ? (1..max) : (1..)` or `Range.new(1, max)` if `(1..nil)` is prohibited. The current behavior allows to just write `(1..max)`. Thus, it was reverted. It is very difficult to change the behavior from now because of the compatibility issue. But as I recall, this is the third time for me to see this issue reported. (The first is #14845. I couldn't find the second but I think someone said it in GitHub comments or else.) If it is a major source of bugs, and if a conditionally endless range is very rare, I'm personally open for the change. ---------------------------------------- Misc #17637: Endless ranges with `nil` boundary weird behavior https://bugs.ruby-lang.org/issues/17637#change-90465 * Author: gud (gud gud) * Status: Open * Priority: Normal ---------------------------------------- Basically it's about this https://andycroll.com/ruby/watch-out-for-nils-in-ranges/ Since Ruby 2.6 we have this weird syntax (0..nil) which is really really bug prone e.g. we have dynamic upper boundary like ``` lower = 0 upper = some_method(arg1, arg2) (lower..upper).each do { |s| some_method2(s) } ``` We rarely do `nil` checks in Ruby so it's really easy to have Infinity loop in the end. Previous Argument error was more intuitive since it throws exception instead of silently looping forever. + some additional strange behavior: ``` (0..nil).count => Infinity (0..Float::INFINITY).count => hangs, I guess same infinity loop ``` Having explicit parameter `Float::INFINITY` (as in previous versions) looks more like a proper design instead of allowing `nil` as a valid parameter. You may think of it as **I would like to have a range from 0 to nothing, what is it actually ?** And I guess the answer is **Nothing**. Fixing `(0..Float::INFINITY).count` this case it also important I believe. Tested on `ruby 2.7.1p83` -- https://bugs.ruby-lang.org/ Unsubscribe: