Interval

While Ruby support the Range class out of the box, is does not quite fullfil the role od a real Interval class. For instance, it does not support excluding the front sentinel. This is because Range also tries to do triple duty as a simple Sequence and as a simple Tuple-Pair, thus limiting its potential as an Interval. The Interval class remedies the situation by commiting to interval behavior, and then extends the class’ capabilites beyond that of the standard Range in ways that naturally fall out of that.

Range depends on two methods: succ and #<=>. If numeric ranges were the only concern, those could just as well be #+ and #<=>, but esoteric forms make that unfeasible —the obvious example being a String range. But a proper Interval class requires mathematical continuation, thus the Interval depends on #+ and #<=>, as well as #- as the inverse of #+.

  i = Interval.new(1,5)
  i.to_a       #=> [1,2,3,4,5]

  i = Interval[0,5]
  i.to_a(2)    #=> [0,2,4]

  i = Interval[1,5]
  i.to_a(-1)   #=> [5,4,3,2,1]

  i = Interval[1,3]
  i.to_a(1,2)  #=> [1.0,1.5,2.0,2.5,3.0]

Authors

  • Thomas Sawyer

Todo

  • Still need to tie in Infinity.

Copying

Copyright (c) 2004 Thomas Sawyer

Ruby License

This module is free software. You may use, modify, and/or redistribute this software under the same terms as Ruby.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Required Files