Module: Hobix::Enumerable

Defined in:
lib/hobix/base.rb

Overview

Enumerable::each_with_neighbors from Joel Vanderwerf’s enum extenstions.

Instance Method Summary collapse

Instance Method Details

#each_with_neighbors(n = 1, empty = nil) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/hobix/base.rb', line 200

def each_with_neighbors n = 1, empty = nil
    nbrs = [empty] * (2 * n + 1)
    offset = n

    each { |x|
        nbrs.shift
        nbrs.push x
        if offset == 0  # offset is now the offset of the first element, x0,
            yield nbrs    #   of the sequence from the center of nbrs, or 0,
        else            #   if x0 has already passed the center.
            offset -= 1
        end
    }

    n.times {
        nbrs.shift
        nbrs.push empty
        if offset == 0
            yield nbrs
        else
            offset -= 1
        end
    }

    self
end