1. 25
  1.  

  2. 10

    Cool, we get some Python slicing tricks now. All you have to do is rotate the colon 90 degrees :-)

    Ruby:

    "abcd"[1..]
    => "bcd" 
    

    Python:

    "abcd"[1:]
    => "bcd" 
    
    1. 5

      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 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.

      1. 2

        Couldn’t you apply a unary minus to the infinite range and get the same thing? Or am I missing something?

        1. 1

          Good point, I hadn’t even thought of the negative infinity case.

        2. 4

          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?

          1. 2

            Nobody likes typing Float::INFINITY or 1.fdiv(0) over and over.

            uhhhhhhhhhhhhhhhhhh

            this seems dangerous

            1. 4

              .. does it?

              1. 2

                OHHHH, fdiv uses floating point semantics I guess. That’s less scary. (And also makes sense - guess I missed the F the first time :))

                1. 2

                  :D Right!

            2. 2

              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?

              1. 2

                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.

                1. 1

                  there are examples in the blog post, but (also as a non ruby programmer) I don’t find any of them particularly compelling

                  1. 1

                    I am a Ruby programmer, and I’m also not sure what people need infinite ranges for so often.

                    1. 1

                      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)

                      1. 1

                        Yeah it’s probably more Ruby-ist to use each_with_index for that, but it’s interesting that that works.