红宝石 |矩阵为空?()函数
empty?()是 Ruby 中的一个内置方法,返回一个布尔值。如果矩阵为空,则返回 true,否则返回 false。
Syntax: mat1.empty?()
Parameters: The function does not accepts any parameter.
Return Value: It returns a boolean value.
示例 1 :
# Ruby program for empty() method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
mat1 = Matrix[[1, 21], [31, 18]]
# prints if empty or not
puts mat1.empty?()
输出:
false
示例 2 :
# Ruby program for empty() method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
mat1 = Matrix[]
# prints if empty or not
puts mat1.empty?()
输出:
true