Ruby | Matrix square?() function Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report The square?() is an inbuilt method in Ruby returns a boolean value. It returns true if it is a square matrix, else it returns false. Syntax: mat1.square?() Parameters: The function needs the matrix to be checked for square matrix or not. Return Value: It returns true if it is a square matrix, else it returns false. Example 1: Ruby # Ruby program for square?() method in Matrix # Include matrix require "matrix" # Initialize a matrix mat1 = Matrix[[3, 12], [2, 8]] # Prints if square? or not puts mat1.square?() Output: true Example 2: Ruby # Ruby program for square?() method in Matrix # Include matrix require "matrix" # Initialize a matrix mat1 = Matrix[[1, 0], [0, 1], [1, 2]] # Prints if square? or not puts mat1.square?() Output: false Comment More infoAdvertise with us Next Article Ruby | Matrix scalar() function G gopaldave Follow Improve Article Tags : Ruby Ruby-Methods Ruby Matrix-class Similar Reads Ruby | Matrix singular?() function The singular?() is an inbuilt method in Ruby returns a boolean value. It returns true if it is a singular matrix, else it returns false. It returns error if anything other than square matrix is used. Syntax: mat1.singular?() Parameters: The function needs the matrix to be checked for singular matrix 1 min read Ruby | Matrix singular?() function The singular?() is an inbuilt method in Ruby returns a boolean value. It returns true if it is a singular matrix, else it returns false. It returns error if anything other than square matrix is used. Syntax: mat1.singular?() Parameters: The function needs the matrix to be checked for singular matrix 1 min read Ruby | Matrix scalar() function The scalar() is an inbuilt method in Ruby returns an N x N diagonal matrix where each diagonal element is value. Syntax: mat1.scalar(N, value) Parameters: The function accepts twos mandatory parameters N and value where N is the size of the Identity matrix and value is the value to be assigned to th 1 min read Ruby | Matrix scalar() function The scalar() is an inbuilt method in Ruby returns an N x N diagonal matrix where each diagonal element is value. Syntax: mat1.scalar(N, value) Parameters: The function accepts twos mandatory parameters N and value where N is the size of the Identity matrix and value is the value to be assigned to th 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 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 Like