红宝石 |矩阵 to_s()函数
to_s()是 Ruby 中的一个内置方法,它返回一个包含矩阵作为对象的字符串。
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.
示例 1 :
# Ruby program for to_s() method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
mat1 = Matrix[[3, 12], [2, 8]]
# Prints the to_s matrix
puts mat1.to_s()
输出:
Matrix[[3, 12], [2, 8]]
示例 2 :
# Ruby program for to_s() method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
mat1 = Matrix[[1, 0], [6, 1], [1, 2]]
# Prints the to_s matrix
puts mat1.to_s()
输出:
Matrix[[1, 0], [6, 1], [1, 2]]