How to find the "effective" end of the range?

.endRuby range is the number used to indicate the end of a range, regardless of whether the range is not included at the end. I don’t understand the rationale for this design decision, but nevertheless I was wondering what is the most idiomatic mechanism for determining the “effective end” of a range (that is, the largest integer nsuch that range.include?(n)there is the trueonly mechanism that I know of is last(1)[0], which seems pretty klunky.

+4
source share
1 answer

Use the method Range#max:

r = 1...10
r.end
# => 10
r.max
# => 9

max , - Float (. range_max range.c ):

(1.0...10.0).max
# TypeError: cannot exclude non Integer end value

(1.0..10.0).max
# => 10.0

, begin > end, nil:

(2.0...1.0).max
# => nil


: max , , end end-1, . O (1), , begin > end, nil. (String, Date,...) , Enumerable#max, !

+7

All Articles