Also (..) doesn't parse, one needs (nil..).
Wow, I think it's absolutely OK that (..)
doesn't parse. Like is it Ruby or https://uk.wikipedia.org/wiki/Brainfuck
And (nil..)
what is that ? Like really if I saw that code 5 years ago without debugging I would say it shouldn't parse as well, my guess would be this is a bug.
My main point is: we already had (0..Float::INFINITY)
in previous versions having that we had proper ArgumentError
on nil
s. So I am not sure why we had to expand the std library for new syntax which bring a lot of confusing e.g.:
a = 1
b = 1
a == b => true
(a...b).size => 0
a = 'a'
b = 'a'
a == b => true
(a...b).size => 0
And now "special case"
a = nil
b = nil
a == b => true
(a...b).size => Infinity
And (1..nil)
iterates for ever which I can read like nil === Float::INFINITY
but actually this returns false
and nil.to_i
returns 0
, but (0..nil.to_i).size
=> 1
If this is not confusing for newcomers then what is ?
I think it's not worth breaking compatibility,
especially considering that third-party libraries most likely already rely on Range#{begin/end} == nil => beginless/endless.
I am trying to think of a good use case for that stuff and I can't find an answer.
If you need an infinite loop
you can always use well known while true
or even loop do
in Ruby. And those are more common and intuitive way to do such a thing.
You can't iterate over nil..nil
because really, what is that ?
Sometimes it's good to revert the stuff even if it's already been using (like pipe operator), still I think the usage of it is not that common.
How often does one actually get bugs based on this?
Not so often but it happens and when it happens it creates a memory leak.
I had a begin/rescue ArugmentError
around my code and had a feeling I was OK and then this happened. BOOM.
I also do have a feeling that regular usage of range e.g. (lower..upper)
is really common in Ruby code, so making this syntax dangerous for a better usability of conditional endless range
which I guess is really rare... feels wrong.
There is a lot of text and please don't get it personal but sometimes I guess this "syntax sugar" stuff drives Ruby in a wrong direction. Aliases/3-4 ways to do the same thing and so on.