What about triple-dot ranges that exclude the end value? The bug report said “I don’t think ary[1...] (exclusive) is meaningful.” Despite this, I see from the commit that endless ranges with triple-dot were implemented. There is only one test case that uses the range in a way other than inspecting its attributes, and that test shows that a 3...nil range iterates over the same numbers as a 3..nil range when passed to Array#fill.
What about infinite ranges in the other direction, (..0) and (nil..0)? They could theoretically be used for checking if a number is less than 100, for example. Well, they are not part of this feature because it would be too hard to implement in Ruby’s grammar:
It is better to have ary[..1] as a consistency. But it will cause a shift/reduce conflict. Anyway, ary[0..1] looks not so bad to me since it have no cursed negative index. So I don’t push it.
Finally! I am so elated about this. Gone are (will be?) the days of 0.0...Float::INFINITY. You will be missed, IEEE 754-1985 \infty, but I think deep down we all thought you were a little… hackish?
as a non ruby programmer I wonder: What do you all need infinite ranges for that you need a special syntax for it. I think I never had a need for that. So I wonder, what are they used for?
Cool, we get some Python slicing tricks now. All you have to do is rotate the colon 90 degrees :-)
Ruby:
Python:
Some extra details from the linked bug report and commit:
What about triple-dot ranges that exclude the end value? The bug report said “I don’t think
ary[1...](exclusive) is meaningful.” Despite this, I see from the commit that endless ranges with triple-dot were implemented. There is only one test case that uses the range in a way other than inspecting its attributes, and that test shows that a3...nilrange iterates over the same numbers as a3..nilrange when passed toArray#fill.What about infinite ranges in the other direction,
(..0)and(nil..0)? They could theoretically be used for checking if a number is less than 100, for example. Well, they are not part of this feature because it would be too hard to implement in Ruby’s grammar:Couldn’t you apply a unary minus to the infinite range and get the same thing? Or am I missing something?
Good point, I hadn’t even thought of the negative infinity case.
Finally! I am so elated about this. Gone are (will be?) the days of
0.0...Float::INFINITY. You will be missed, IEEE 754-1985\infty, but I think deep down we all thought you were a little… hackish?uhhhhhhhhhhhhhhhhhh
this seems dangerous
.. does it?
OHHHH, fdiv uses floating point semantics I guess. That’s less scary. (And also makes sense - guess I missed the F the first time :))
:D Right!
as a non ruby programmer I wonder: What do you all need infinite ranges for that you need a special syntax for it. I think I never had a need for that. So I wonder, what are they used for?
How about a game loop that keeps track of ticks?
As a Ruby programmer I have definitely written infinite ranges a handful of times - it does come up, though it’s not common.
there are examples in the blog post, but (also as a non ruby programmer) I don’t find any of them particularly compelling
I am a Ruby programmer, and I’m also not sure what people need infinite ranges for so often.
I use them, but that’s probably me porting Hasellisms to Ruby more than a common Rubyism. Things like:
(0..1.0/0).zip(array)Yeah it’s probably more Ruby-ist to use
each_with_indexfor that, but it’s interesting that that works.