Class: Susanin::Pattern
- Inherits:
-
Object
- Object
- Susanin::Pattern
- Includes:
- Enumerable
- Defined in:
- lib/susanin/pattern.rb
Instance Method Summary collapse
- #arr_size ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(arr) ⇒ Pattern
constructor
A new instance of Pattern.
- #inspect ⇒ Object
- #shifts ⇒ Object
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_size ⇒ Object
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 |
#inspect ⇒ Object
31 32 33 |
# File 'lib/susanin/pattern.rb', line 31 def inspect map{|i|i} end |
#shifts ⇒ Object
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 |