Class: Mir::Grid
- Inherits:
-
Object
- Object
- Mir::Grid
- Defined in:
- lib/mir/01_model.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#size ⇒ Object
Returns the value of attribute size.
Class Method Summary collapse
Instance Method Summary collapse
- #[](y, x) ⇒ Object
- #[]=(y, x, v) ⇒ Object
- #each(&block) ⇒ Object
- #fill_with(v) ⇒ Object
-
#initialize(width, height) ⇒ Grid
constructor
A new instance of Grid.
- #print ⇒ Object
- #to_a ⇒ Object
Constructor Details
Instance Attribute Details
#size ⇒ Object
Returns the value of attribute size.
48 49 50 |
# File 'lib/mir/01_model.rb', line 48 def size @size end |
Class Method Details
.from_a(ary1d, sx, sy) ⇒ Object
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/mir/01_model.rb', line 88 def self.from_a ary1d,sx,sy ret=self.new(sx,sy) sy.times do |y| sx.times do |x| index=y*(sx)+x ret[y,x]=ary1d[index] end end ret end |
.random(sx, sy, klass) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/mir/01_model.rb', line 99 def self.random sx,sy,klass ret=self.new(sx,sy) sy.times do |y| sx.times do |x| ret[y,x]=klass.random end end ret end |
Instance Method Details
#[](y, x) ⇒ Object
54 55 56 |
# File 'lib/mir/01_model.rb', line 54 def [](y,x) @values[y][x] end |
#[]=(y, x, v) ⇒ Object
58 59 60 |
# File 'lib/mir/01_model.rb', line 58 def []=(y,x,v) @values[y][x]=v end |
#each(&block) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/mir/01_model.rb', line 66 def each &block @values.each do |row| row.each do |v| yield v end end end |
#fill_with(v) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/mir/01_model.rb', line 74 def fill_with v @values.each_with_index do |row,y| row.each_with_index do |pix,x| self[y,x]=v end end end |
#print ⇒ Object
82 83 84 85 86 |
# File 'lib/mir/01_model.rb', line 82 def print @values.each do |row| puts row.map{|pix| pix.to_s.rjust(3)}.join(" ") end end |
#to_a ⇒ Object
62 63 64 |
# File 'lib/mir/01_model.rb', line 62 def to_a @values.flatten end |