Snippets

ESTI design dirty, hacky solution of pattern drawing problem

Created by Robert Poz last modified
PATTERN=%q[
  |  
  \  
-\ \-
  \  
]

def draw(w, h, rows, cols)
  matrix =  PATTERN.gsub('  ', ' '*(w/2+1)).gsub('-',  '-'*(w/2)).
            split("\n").map { |r| r.split('') }.reject(&:empty?)
  [:push, :unshift].each { |m| matrix.send(m, *[matrix.first]*(h/2)) }
  matrix.shift and matrix.map { |r| (r*(cols+1)).join }*(rows+1)
end

puts draw(4, 4, 4, 5)
puts draw(6, 4, 3, 4)

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.