From: Brian Adkins Date: 2007-10-13T11:45:02+09:00 Subject: Re: range max On Oct 12, 9:59 pm, Brian Adkins wrote: > On Oct 12, 1:25 pm, "ara.t.howard" wrote: > > > On Oct 12, 2007, at 9:40 AM, Brian Adkins wrote: > > > Actually, what you want is simply N. It's a nonsensical request from a > > > troll. Using max, end, last, etc. to find the end of the range is > > > ridiculous since you must know the end of the range to define the > > > range! > > > that is not true. any object can be used in a range in ruby. it > > only must respond to #succ. it's quite possible to declare classes > > that worked like so > > I'm not unfamiliar with the domain of the Range class. Let's not get > ridiculous here; I was referring specifically to integer ranges. > Nothing unambiguates like code, eh? :) > > class Range > def max_int > raise 'invalid range' if self.end < self.begin > self.end - (self.exclude_end? ? 1 : 0) > end > end Actually, I think the following would be better for those rare people who find themselves frequently wondering what the largest int in a range is, and would prefer O(1) instead of O(n): class Range alias orig_max max def max if ( self.begin.kind_of?(Integer) && self.end.kind_of?(Integer) && self.begin <= self.end ) self.exclude_end? ? self.end - 1 : self.end else orig_max end end end Now if someone can just come up with a real world example (i.e. currently existing code in production) of Ruby code that actually requires this, I'll be amazed. :)