Class: PictureFrame::Stencil

Inherits:
Raster
  • Object
show all
Defined in:
lib/picture_frame/stencil.rb

Instance Method Summary collapse

Methods inherited from Raster

#==, #at, #bottom_right, #dimensions, #each, #each_with_index, #height, #slice, #to_s, #top_left, #width

Constructor Details

#initializeStencil

Returns a new instance of Stencil.



5
6
7
# File 'lib/picture_frame/stencil.rb', line 5

def initialize(*)
  super
end

Instance Method Details

#position(char) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/picture_frame/stencil.rb', line 9

def position(char)
  @raster.each_with_index do |row, ri|
    row.chars.with_index do |cell, ci|
      return ri, ci if cell == char
    end
  end
  []
end

#slice_from(pos, to) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/picture_frame/stencil.rb', line 18

def slice_from(pos, to)
  r, c = pos
  n, m = bottom_right

  case to
  when :t, :top
    slice([0, c], [r - 1, c])
  when :r, :right
    slice([r, c + 1], [r, m])
  when :b, :bottom
    slice([r + 1, c], [n, c])
  when :l, :left
    slice([r, 0], [r, c - 1])

  when :tl, :top_left
    slice([0, 0], [r - 1, c - 1])
  when :tr, :top_right
    slice([0, c + 1], [r - 1, m])
  when :br, :bottom_right
    slice([r + 1, c + 1], [n, m])
  when :bl, :bottom_left
    slice([r + 1, 0], [n, c - 1])
  end
end