📜  红宝石 |数组类为空?() 操作

📅  最后修改于: 2022-05-13 01:55:47.692000             🧑  作者: Mango

红宝石 |数组类为空?() 操作

Array#empty?() : empty?()是一个 Array 类方法,用于检查数组是否为空。

代码 #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