Class: Susanin::Pattern

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/susanin/pattern.rb

Instance Method Summary collapse

Constructor Details

#initialize(arr) ⇒ Pattern

Returns a new instance of Pattern.



8
9
10
# File 'lib/susanin/pattern.rb', line 8

def initialize(arr)
  @arr = Array.wrap(arr)
end

Instance Method Details

#arr_sizeObject



19
20
21
# File 'lib/susanin/pattern.rb', line 19

def arr_size
  @arr.size
end

#each(&block) ⇒ Object



12
13
14
15
16
17
# File 'lib/susanin/pattern.rb', line 12

def each(&block)
  shifts.map do |s|
    a = @arr.slice(*s)
    block.call(a.size == 1 ? a[0] : a)
  end
end

#inspectObject



31
32
33
# File 'lib/susanin/pattern.rb', line 31

def inspect
  map{|i|i}
end

#shiftsObject



23
24
25
26
27
28
29
# File 'lib/susanin/pattern.rb', line 23

def shifts
  arr_size.downto(1).flat_map do |shift|
    (0).upto(arr_size-shift).map do |i|
      [0+i, shift]
    end
  end
end