Forum: Ruby Specializing Range

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Brian B. (Guest)
on 2006-02-18 20:30
(Received via mailing list)

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.
max implementation is to iterate though the entire range to determine
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

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

This topic is locked and can not be replied to.