Class: Doku::DancingLinks::LinkEnumerator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/doku/dancing_links.rb

Overview

This is a class that lets us concisely enumerate a certain set of objects by traveling either up, down, left, or right from a starting object until we wrap around and reach that same node. Since this class includes the Enumerable class, it has several fancy methods available with it such as “max_by”, “collect”, or “to_a”.

Instance Method Summary collapse

Constructor Details

#initialize(link, start, include_start = false) ⇒ LinkEnumerator

Returns a new instance of LinkEnumerator.

Parameters:



38
39
40
# File 'lib/doku/dancing_links.rb', line 38

def initialize(link, start, include_start=false)
  @link, @start, @include_start = link, start, include_start
end

Instance Method Details

#each {|obj| ... } ⇒ Object

Iterates through objects by starting at the starting object and going in the specified direction until the start point is found again. The starting object will be yielded first, if this LinkEnumerator was configured to yield it #initialize.

Yields:

  • (obj)


48
49
50
51
52
53
54
55
56
57
# File 'lib/doku/dancing_links.rb', line 48

def each
  yield @start if @include_start

  n = @start
  while true
    n = n.send @link
    return if n == @start
    yield n
  end
end