Method: Matrix#collect!
- Defined in:
- lib/matrix.rb
#collect!(which = :all) ⇒ Object Also known as: map!
Invokes the given block for each element of matrix, replacing the element with the value returned by the block. Elements can be restricted by passing an argument:
-
:all (default): yields all elements
-
:diagonal: yields only elements on the diagonal
-
:off_diagonal: yields all elements except on the diagonal
-
:lower: yields only elements on or below the diagonal
-
:strict_lower: yields only elements below the diagonal
-
:strict_upper: yields only elements above the diagonal
-
:upper: yields only elements on or above the diagonal
515 516 517 518 519 |
# File 'lib/matrix.rb', line 515 def collect!(which = :all) return to_enum(:collect!, which) unless block_given? raise FrozenError, "can't modify frozen Matrix" if frozen? each_with_index(which){ |e, row_index, col_index| @rows[row_index][col_index] = yield e } end |