-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.rb
40 lines (33 loc) · 870 Bytes
/
block.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class StockingStitchBlock
def initialize(rows: nil, starting_stitch: :purl, stitches: nil)
self.starting_stitch = starting_stitch
self.rows = rows
self.stitches = stitches
end
def to_pattern
puts "Stocking Stitch for #{rows} rows: start with #{starting_stitch.to_s}"
end
def to_ascii
rows_in_ascii.each do |a|
GingerbreadMan.put_in_center a.center(150)
end
end
private
def rows_in_ascii
rows.times.each_with_index.flat_map do |row, idx|
if idx.even?
Row.new(even_stitch_obj.new(stitches)).ascii
else
Row.new(odd_stitch_obj.new(stitches)).ascii
end
end
end
def even_stitch_obj
Kernel.const_get(starting_stitch.to_s.capitalize)
end
def odd_stitch_obj
return Knit if starting_stitch == :purl
Purl
end
attr_accessor :rows, :starting_stitch, :stitches
end