Ruby | Matrix zero() function Last Updated : 12 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The zero() is an inbuilt method in Ruby returns an N x M zero matrix where each element is zero. Syntax: mat1.zero(N, M) Parameters: The function accepts two mandatory parameters N and M which is the size of the matrix. Return Value: It returns the zero matrix of size N x M. Example 1: Ruby # Ruby program for zero() method in Matrix # Include matrix require "matrix" # Initialize a matrix # using zero method mat1 = Matrix.zero(4 , 3) # Print the matrix puts mat1 Output: Matrix[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]] Example 2: Ruby # Ruby program for zero() method in Matrix # Include matrix require "matrix" # Initialize a matrix # using zero method mat1 = Matrix.zero(2, 2) # Print the matrix puts mat1 Output: Matrix[[0, 0], [0, 0]] Comment More infoAdvertise with us Next Article Ruby | Matrix zero?() function G gopaldave Follow Improve Article Tags : Ruby Ruby-Methods Ruby Matrix-class Similar Reads Ruby | Matrix zero?() function The zero?() is an inbuilt method in Ruby returns a boolean value. It returns true if it is a zero matrix, else it returns false. Zero matrix means that the matrix has all itselements as 0.Syntax: mat1.zero?()Parameters: The function needs the matrix to be checked for zero matrix.Return Value: It ret 1 min read Ruby | Matrix tr() function The tr() is an inbuilt method in Ruby returns the trace i.e., sum of diagonal elements of the matrix. Syntax: mat1.tr() Parameters: The function needs the matrix whose trace is to be returned. Return Value: It returns the trace. Example 1: Ruby # Ruby program for tr() method in Matrix # Include matr 1 min read Ruby | Matrix t() function The t() is an inbuilt method in Ruby returns the transpose of the matrix. Syntax: mat1.t() Parameters: The function needs the matrix to be transposed. Return Value: It returns the transposed matrix. Example 1: Ruby # Ruby program for t() method in Matrix # Include matrix require "matrix" # 1 min read Ruby | Matrix to_a() function The to_a() is an inbuilt method in Ruby returns an array which has all the elements of the matrix row-wise. Syntax: mat1.to_a() Parameters: The function needs the matrix which is to be converted to an array. Return Value: It returns an array. Example 1: Ruby # Ruby program for to_a() method in Matri 1 min read Ruby | Matrix to_s() function The to_s() is an inbuilt method in Ruby returns a string containing the matrix as the object. Syntax: mat1.to_s() Parameters: The function needs the matrix whose string object is to be returned. Return Value: It returns a string or self. Example 1: Ruby # Ruby program for to_s() method in Matrix # I 1 min read Ruby | Numeric zero? function The zero?() is an inbuilt method in Ruby returns a boolean value. It returns true if the number is a negative one, else it returns false. Syntax: num.zero?() Parameters: The function needs a number which is to be checked for. Return Value: It returns returns a boolean value. Example 1: CPP # Ruby pr 1 min read Like