def isfin? @matrix[0..@col_count-1][0]==0 end 

How to use Ruby's iterators? For example, you need to return true if all elements of the array [n] [0] (where n from 0 to "array length - 1") are equal to zero. Or is it better to use a simple loop in this case?

For example, if we have 0..200 , and the second position is no longer 0 , then you can exit with false and not view the other elements. Or when using iterators, this condition will be automatically taken into account, and will not work more slowly?

    1 answer 1

    First, judging by the dog, we are talking about the method of its own class. I would recommend not returning any OOP until you know the language and stdlib itself.

     def isfin? @matrix.all? do |row| row[0] == 0 end end 

    Iterator all? here it breaks and returns false as soon as the do end block returns false .

    You can rewrite it in such a way that the matrix is ​​first transposed, and we check only one-dimensional array:

     def isfin? @matrix.transpose.first.all? do |cell| cell == 0 end end 

    And then use the standard .zero? method .zero? lets write it shorter:

     def isfin? @matrix.transpose.first.all? &:zero? end 

    And you can use .map instead of .tranpose use - it can work faster:

     def isfin? @matrix.map(&:first).all? &:zero? end 
    • one
      Great! Many thanks for such a reviewer, very informative! - Isaev