Ruby 数组类的empty?()操作
Array#empty?() : empty?()是一个数组类方法,用于检查数组是否为空。
语法。Array.empty?()
参数。数组
返回:true – 如果数组中没有元素;否则为false。
代码 #1 : empty?() 方法的例子
# Ruby code for empty?() method
# declaring array
a = [18, 22, 33, nil, 5, 6]
# declaring array
b = [1, 4, 1, 1, 88, 9]
# declaring array
c = []
# empty?
puts "empty? : #{a.empty?()}\n\n"
# empty?
puts "empty? : #{b.empty?()}\n\n"
# empty?
puts "empty? : #{c.empty?()}\n\n"
输出:
empty? : false
empty? : false
empty? : true
代码 #2 : empty?()方法的例子
# Ruby code for empty?() method
# declaring array
a = ["abc", "nil", "dog"]
# declaring array
b = ["cow", nil, "dog"]
# declaring array
c = ["cat", nil, nil]
# empty?
puts "empty? : #{a.empty?()}\n\n"
# empty?
puts "empty? : #{b.empty?()}\n\n"
# empty?
puts "empty? : #{c.empty?()}\n\n"
输出:
empty? : false
empty? : false
empty? : false