Open In App

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]]

Similar Reads