Forum: Ruby Specializing Range

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9a46755c817121b0571a563e5ab02090?d=identicon&s=25 Brian Buckley (brian)
on 2006-02-18 19:30
(Received via mailing list)
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
This topic is locked and can not be replied to.