Specializing Range


#1

Hello,

I was using Dates in a Range and my program was running very slowly. I
realized the cause was my calling Range#max when the range was large.
The
max implementation is to iterate though the entire range to determine
what
the max is. That’s unnecessary for Date (and many other types for that
matter) so I added the following to Range

class Range
alias_method :old_max, :max
def max
(Date === first and first < last) ? last - 1 : old_max
end
end

It works (now my program runs fast) but is it The Ruby Way to specialize
a
standard container like Range for a specific type like this? Is it a
bad
idea? Are there hidden gotchas I’m not seeing?

Brian