Class: Diagrams::Grid
Instance Attribute Summary collapse
-
#height ⇒ Object
Returns the value of attribute height.
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
- #each ⇒ Object
- #get(x, y) ⇒ Object
-
#initialize(width, height, init) ⇒ Grid
constructor
A new instance of Grid.
- #inspect ⇒ Object
- #inspect2(valid) ⇒ Object
- #read_area(x, y, w, h) ⇒ Object
- #set(x, y, v) ⇒ Object
- #set_area(x, y, w, h, e) ⇒ Object
Constructor Details
Instance Attribute Details
#height ⇒ Object
Returns the value of attribute height.
4 5 6 |
# File 'lib/maruku/ext/diagrams/grid.rb', line 4 def height @height end |
#width ⇒ Object
Returns the value of attribute width.
4 5 6 |
# File 'lib/maruku/ext/diagrams/grid.rb', line 4 def width @width end |
Instance Method Details
#each ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/maruku/ext/diagrams/grid.rb', line 49 def each for y in 0..(height-1) for x in 0..(width-1) e = get(x,y) yield x,y,e end end end |
#get(x, y) ⇒ Object
13 14 15 |
# File 'lib/maruku/ext/diagrams/grid.rb', line 13 def get(x, y) @data[y][x] end |
#inspect ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/maruku/ext/diagrams/grid.rb', line 21 def inspect @data.map{|x| x.map{|y| case y when true; '#' when false, nil; ' ' when Fixnum; y.chr end }.join }.join("\n") end |
#inspect2(valid) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/maruku/ext/diagrams/grid.rb', line 33 def inspect2(valid) s = "" s += "+" + "-"*@width + "+\n" for y in 0..(@height-1) s += "|" for x in 0..(@width-1) s += valid.get(x,y) ? get(x,y).chr : " " end s += "|" s += "\n" end s += "+" + "-"*@width + "+\n" s end |
#read_area(x, y, w, h) ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/maruku/ext/diagrams/grid.rb', line 66 def read_area(x,y,w,h) s = "" for j in (y..y+h-1) for i in (x..x+w-1) s << get(i,j) end s << "\n" end s end |
#set(x, y, v) ⇒ Object
17 18 19 |
# File 'lib/maruku/ext/diagrams/grid.rb', line 17 def set(x, y, v) @data[y][x] = v || @init end |
#set_area(x, y, w, h, e) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/maruku/ext/diagrams/grid.rb', line 58 def set_area(x,y,w,h,e) for i in (x..x+w-1) for j in (y..y+h-1) set(i,j,e) end end end |