Why (10..20). Coincides with (10 ... 20) .last

Why are these two equivalents?

(10..20).last #=> 20 (10...20).last #=> 20 

It sounds like a duplicate of Ruby 'Range.last' does not give the last meaning. Why? but the answers to this question just talk about it in design. Why is it so arranged? What is the purpose .. and ... returning the same values ​​for last when everything else is different?

+5
source share
1 answer

I will answer your question with the question: what is the last value of 0.0...1.0 ?

In general, not every range is enumerable . For such excluded ranges, there really is no significant last value other than the value used to determine the end of the range.

Note that you can list ranges inclusively , where last not the last value listed!

 (0..3.2).to_a.last # 3 (0..3.2).last # 3.2 (0..Float::INFINITY).to_a.last # don't wait up for this one 

The design motivation is that the “value used to determine the end of the range” is not identical to the “last value specified by the range”. last gives you the first.

+7
source

Source: https://habr.com/ru/post/1202979/


All Articles