Class: RichEngine::Matrix
- Inherits:
-
Object
- Object
- RichEngine::Matrix
- Defined in:
- lib/rich_engine/matrix.rb
Overview
A simple 2D grid utility backed by nested arrays, with convenience methods for indexing, iterating, mapping, zipping, and filling regions.
Instance Attribute Summary collapse
-
#vec ⇒ Array<Array>
The backing nested array of rows.
Instance Method Summary collapse
-
#[](x, y) ⇒ Object
Reads the value at the given coordinates.
-
#[]=(x, y, value) ⇒ Object
Writes a value at the given coordinates.
-
#any? {|cell| ... } ⇒ Boolean
Whether any cell matches the given block.
-
#each {|tile| ... } ⇒ void
Iterates over every cell in row-major order.
-
#each_with_indexes {|tile, i, j| ... } ⇒ void
Iterates over every cell along with its column and row indexes.
-
#fill(x:, y:, with:) ⇒ void
Fills a cell or region with a value.
-
#initialize(width: 1, height: 1, fill_with: nil) ⇒ Matrix
constructor
Builds a matrix of the given dimensions, filling every cell with +fill_with+.
-
#map {|value| ... } ⇒ Array<Array>
Maps every cell through the block, returning a nested array of results.
-
#zip(other) ⇒ Matrix
Pairs each cell with the cell at the same coordinates in +other+.
Constructor Details
#initialize(width: 1, height: 1, fill_with: nil) ⇒ Matrix
Builds a matrix of the given dimensions, filling every cell with +fill_with+.
23 24 25 |
# File 'lib/rich_engine/matrix.rb', line 23 def initialize(width: 1, height: 1, fill_with: nil) @vec = Array.new(width) { Array.new(height) { fill_with } } end |
Instance Attribute Details
#vec ⇒ Array<Array>
Returns the backing nested array of rows.
15 16 17 |
# File 'lib/rich_engine/matrix.rb', line 15 def vec @vec end |
Instance Method Details
#[](x, y) ⇒ Object
Reads the value at the given coordinates.
32 33 34 |
# File 'lib/rich_engine/matrix.rb', line 32 def [](x, y) @vec[x][y] end |
#[]=(x, y, value) ⇒ Object
Writes a value at the given coordinates.
42 43 44 |
# File 'lib/rich_engine/matrix.rb', line 42 def []=(x, y, value) @vec[x][y] = value end |
#any? {|cell| ... } ⇒ Boolean
Whether any cell matches the given block.
50 51 52 |
# File 'lib/rich_engine/matrix.rb', line 50 def any?(&block) @vec.any? { |row| row.any?(&block) } end |
#each {|tile| ... } ⇒ void
This method returns an undefined value.
Iterates over every cell in row-major order.
58 59 60 61 62 63 64 |
# File 'lib/rich_engine/matrix.rb', line 58 def each @vec.each do |row| row.each do |tile| yield(tile) end end end |
#each_with_indexes {|tile, i, j| ... } ⇒ void
This method returns an undefined value.
Iterates over every cell along with its column and row indexes.
95 96 97 98 99 100 101 |
# File 'lib/rich_engine/matrix.rb', line 95 def each_with_indexes @vec.each_with_index do |row, i| row.each_with_index do |tile, j| yield(tile, i, j) end end end |
#fill(x:, y:, with:) ⇒ void
This method returns an undefined value.
Fills a cell or region with a value. +x+ and +y+ may each be a single index or any object responding to +each+ (e.g. a Range), so regions can be filled in one call.
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/rich_engine/matrix.rb', line 113 def fill(x:, y:, with:) xs = Iterable(x) ys = Iterable(y) xs.each do |x| ys.each do |y| @vec[x][y] = with end end end |
#map {|value| ... } ⇒ Array<Array>
Maps every cell through the block, returning a nested array of results.
70 71 72 73 74 |
# File 'lib/rich_engine/matrix.rb', line 70 def map(&block) @vec.map do |row| row.map { |value| block.call(value) } end end |
#zip(other) ⇒ Matrix
Pairs each cell with the cell at the same coordinates in +other+.
81 82 83 84 85 86 87 88 |
# File 'lib/rich_engine/matrix.rb', line 81 def zip(other) new_matrix = Matrix.new new_matrix.vec = @vec.map.with_index do |row, i| row.map.with_index { |value, j| [value, other[i, j]] } end new_matrix end |