Last Updated: February 25, 2016
·
1.251K
· demiazz

Ruby ranges and upper limits

If you want get maximum value of Range in Ruby, don't use Range#last.

Method Range#last returns upper limit point for any ranges:

(1..5).last #=> 5
(1...5).last #=> 5

If you use 1.8.7 or later, you need use next snippet:

range.exclude_end? ? range.last - 1 : range.last

If you use 1.9.x and higher, simple use Range#max method.