红宝石 |数组一?()函数
Array#one?() : one?()是一种 Array 类方法,用于检查数组是否只有一个数组元素。
Syntax: Array.one?()
Parameter: Array
Return: true – if array has only one element otherwise return false.
示例 #1:
# Ruby code for one?() method
# declaring array
a = [18, 22, 33, nil, 5, 6]
# declaring array
b = [1, 4, 1, 1, 88, 9]
# declaring array
c = [2]
# one? method example
puts "one?() method form : #{a.one?()}\n\n"
puts "one?() method form : #{b.one?()}\n\n"
puts "one?() method form : #{c.one?()}\n\n"
输出 :
one?() method form : false
one?() method form : false
one?() method form : true
示例 #2:
# Ruby code for one?() method
# declaring array
a = ["abc", "nil", "dog"]
# declaring array
c = ["cow"]
# declaring array
b = ["cow", nil, "dog"]
# one? method example
puts "one?() method form : #{a.one?()}\n\n"
puts "one?() method form : #{b.one?()}\n\n"
puts "one?() method form : #{c.one?()}\n\n"
输出 :
one?() method form : false
one?() method form : false
one?() method form : true