# Specializing Range

**URL:** <https://www.ruby-forum.com/t/specializing-range/70132>\
**Category:** Ruby\
**Created:** [February 18, 2006, 7:30pm UTC](https://www.ruby-forum.com/t/specializing-range/70132 "2006-02-18T19:30:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![brian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/97f17d/32.png) [@brian](https://www.ruby-forum.com/u/brian)\
**Post date:** [February 18, 2006, 7:30pm UTC](https://www.ruby-forum.com/t/specializing-range/70132/1 "2006-02-18T19:30:06Z")

</div>

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
