From 37585800b44453752e993369967a35e1b68dfc58 Mon Sep 17 00:00:00 2001 From: ddrkt Date: Sun, 3 Nov 2019 20:52:44 +0300 Subject: [PATCH] feat(collect!): collected! and tests implemented --- lib/matrix/matrix.rb | 9 +++++++++ test/matrix/matrix_test.rb | 11 +++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/matrix/matrix.rb b/lib/matrix/matrix.rb index b61ec4c..5271ede 100644 --- a/lib/matrix/matrix.rb +++ b/lib/matrix/matrix.rb @@ -27,6 +27,15 @@ def collect collected_rows end + def collect! + rows.each_with_index do |row, i| + row.each_with_index do |col, j| + self[i, j] = yield(col) + end + end + self + end + # # Overrides Object#to_s # diff --git a/test/matrix/matrix_test.rb b/test/matrix/matrix_test.rb index 4f83e09..45f065e 100644 --- a/test/matrix/matrix_test.rb +++ b/test/matrix/matrix_test.rb @@ -67,5 +67,16 @@ def test_to_str m1 = Matrix[[1, 2], [3, 4]] assert_equal "FastMatrix::Matrix[[1.0, 2.0], [3.0, 4.0]]", m1.to_str end + + def test_collect + m1 = Matrix[[1, 2], [3, 4]] + assert_equal m1.collect { |el| el.join(',') }, ["1.0,2.0", "3.0,4.0"] + end + + def test_collect! + m1 = Matrix[[1, 2], [3, 4]] + m1.collect! { |el| el*2 } + assert_equal m1, Matrix[[2, 4], [6, 8]] + end end end