Collections::Grid(T)
Constructors
Class methods
Builds a grid from a multi-line string, mapping each character to a cell
value via the block. Each line becomes a row (x), each column a y; the
grid width is the length of the longest line, and shorter rows are left at
default_value.
grid = Collections::Grid(Int32).from_string("12\n34", 0) { |char, _x, _y| char.to_i }
grid.get(1, 1) # => 4
Instance methods
Flood fills the connected region of cells that share the start cell's
value, replacing each with new_value, and returns the filled points in
fill order. Connectivity is orthogonal by default; pass diagonal: true
to also spread across diagonals.
Cells are matched by value (not by blocked?), so this works on any grid.
Get valid neighbors for the given cell. Orthogonal (up/down/left/right)
by default; pass diagonal: true to also include the four diagonal cells.
Pass toroidal: true to treat the grid as a torus: neighbors wrap across
the edges (via #wrap) instead of being clipped. On small grids where
opposite neighbors land on the same cell, the result is deduplicated and
the origin cell itself is never included.
Returns the connected region of cells that share the start cell's value,
in traversal order, without modifying the grid. Connectivity is orthogonal
by default; pass diagonal: true to also spread across diagonals.
Cells are matched by value (not by blocked?), so this works on any grid.