Class Interval
In: lib/more/facets/interval.rb
Parent: Object

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]

Methods

+@   -@   ===   []   begin   closed   degenerate?   direction   distance   each   end   eql?   exclude_begin?   exclude_end?   exclude_first?   exclude_last?   first   first_closed   first_opened   half_closed   include?   last   last_closed   last_opened   length   max   member?   min   new   null?   opened   reversed   sentinels   size   step   ~  

Included Modules

Multiton Enumerable::Arguments

Public Class methods

Public Instance methods

Unary shorthands. These return a new interval exclusive of first, last or both sentinels, repectively.

===(x)

Alias for include?

begin()

Alias for first

Returns a new interval inclusive of of both sentinels.

Returns true if the start and end sentinels are equal and the interval is closed; otherwise false.

Returns the direction of the interval indicated by +1, 0 or -1.

  (1..5).direction  #=> 1
  (5..1).direction  #=> -1
  (1..1).direction  #=> 0

Returns the length of the interval as the difference between the first and last elements. Returns nil if the sentinal objects do not support distance comparison (distance). TODO: Add n parameter to count segmentations like those produced by each.

Iterates over the interval, passing each _n_th element to the block. If n is not given then n defaults to 1. Each _n_th step is determined by invoking +++ or +\-+ n, depending on the direction of the interval. If n is negative the iteration is preformed in reverse form end sentinal to front sentinal. A second parameter, d, can be given in which case the applied step is calculated as a fraction of the interval‘s length times n / d. This allows iteration over the whole interval in equal sized segments.

  1..5.each { |e| ... }        #=> 1 2 3 4 5
  1..5.each(2) { |e| ... }     #=> 1 3 5
  1..5.each(1,2) { |e| ... }   #=> 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
end()

Alias for last

Compares two intervals to see if they are equal

exclude_begin?()

Alias for exclude_first?

exclude_end?()

Alias for exclude_last?

Returns the first or last sentinal of the interval.

Returns a new interval with one of the two sentinels opened or closed

Returns a new interval with either the first or the last sentinel exclusive. If the parameter is false, the deafult, then the first sentinel is excluded; if the parameter is true, the last sentinel is excluded.

Returns true or false if the element is part of the interval.

length()

Alias for distance

Returns the greater of the first and last sentinals.

member?(x)

Alias for include?

Returns the lesser of the first and last sentinals.

Returns true if the start and end sentinels are equal and the interval is open; otherwise false.

Returns a new interval exclusive of both sentinels.

Returns a new interval with the sentinels reversed.

  (0..10).reversed  #=> 10..0

Returns a two element array of first and last sentinels.

 (0..10).sentinels   #=> [0,10]
size()

Alias for distance

step(n=1, d=nil)

Alias for each

[Validate]