Method: Grid#map_a
- Defined in:
- lib/cem/cruzzles.rb
#map_a(&block) ⇒ Object
Maps each cell of the grid by the given block.
If block has…
- one parameter, passes the cell value
- two parameters, passes the coordinates as a Point2D and the cell value
- three parameteres, passes y, x and the cell value
If the block returns nil, the original value is kept.
525 526 527 528 529 530 531 532 533 534 535 536 537 538 |
# File 'lib/cem/cruzzles.rb', line 525 def map_a(&block) case block.arity when 1 @data.map { |row| row.map { |value| block.call(value) || value } } when 2 @data.each_with_index.map { |row, y| row.each_with_index.map { |value, x| block.call(Point2D.new(x, y), value) || value } } when 3 @data.each_with_index.map { |row, y| row.each_with_index.map { |value, x| block.call(y, x, value) || value} } else raise end end |