From: marcandre-ruby-core@... Date: 2020-12-16T15:47:24+00:00 Subject: [ruby-core:101467] [Ruby master Feature#17277] Make Enumerator#with_index yield row and col indices for Matrix Issue #17277 has been updated by marcandre (Marc-Andre Lafortune). Status changed from Assigned to Closed Closing for lack of a viable solution ---------------------------------------- Feature #17277: Make Enumerator#with_index yield row and col indices for Matrix https://bugs.ruby-lang.org/issues/17277#change-89242 * Author: greggzst (Grzegorz Jakubiak) * Status: Closed * Priority: Normal * Assignee: marcandre (Marc-Andre Lafortune) ---------------------------------------- Given a matrix: ```ruby matrix = Matrix[[0,2,3,4], [6,7,8,9], [1,4,5,8]] ``` You could get the row and col indices of a matrix using `Matrix#each_with_index`: ```ruby matrix .each_with_index { |e, row, col| p [row, col] } [0, 0] [0, 1] [0, 2] [0, 3] [1, 0] [1, 1] [1, 2] [1, 3] [2, 0] [2, 1] [2, 2] [2, 3] ``` You can chain it with other enumerators and access indices within them: ```ruby matrix .each_with_index .filter_map { |e, row, col| [row, col] if e % 4 == 0} # => [[0, 0], [0, 3], [1, 2], [2, 1], [2, 3]] ``` Meanwhile, `with_index` after `Matrix#each` returns flattened indices, not row or column indices, which does not look right: ```ruby matrix .each.with_index { |e, index| p index } 0 1 2 3 4 5 6 7 8 9 10 11 ``` I feel we should override `with_index` for `Matrix` so it returns row and column indices. -- https://bugs.ruby-lang.org/ Unsubscribe: