Ruby | Matrix to_matrix() function Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report The to_matrix() is an inbuilt method in Ruby returns a matrix which is self only. Syntax: mat1.to_matrix() Parameters: The function needs the matrix which is to be returned. Return Value: It returns an matrix, or self. Example 1: Ruby # Ruby program for to_matrix() method in Matrix # Include matrix require "matrix" # Initialize a matrix mat1 = Matrix[[3, 12], [2, 8]] # Prints the to_matrix matrix puts mat1.to_matrix() Output: Matrix[[3, 12], [2, 8]] Example 2: Ruby # Ruby program for to_matrix() method in Matrix # Include matrix require "matrix" # Initialize a matrix mat1 = Matrix[[1, 0], [6, 1], [1, 2]] # Prints the to_matrix matrix puts mat1.to_matrix() Output: Matrix[[1, 0], [6, 1], [1, 2]] Comment More infoAdvertise with us Next Article Ruby | Matrix t() function G gopaldave Follow Improve Article Tags : Ruby Ruby-Methods Ruby Matrix-class Similar Reads 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 I() function The I() is an inbuilt method in Ruby returns a Identity matrix of N X N size. Syntax: mat1.I(N) Parameters: The function accepts a mandatory parameter N which is the size of the Identity matrix. Return Value: It returns the Identity matrix. Example 1: Ruby # Ruby program for I() method in Matrix # I 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 | Matrix imag() function The imag() is an inbuilt method in Ruby returns a matrix with only imaginary part in it. The other index are assigned to zero. Syntax: mat1.imag() Parameters: The function does not takes any parameter. Return Value: It returns a matrix with only imaginary part. Example 1: CPP #Ruby program for imag( 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