t7c04g
Last Updated: February 25, 2016
·
940
· demiazz
71e9761dce27485eb3ced22ed60f59d1

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.

Say Thanks
Respond