Skip to content
Related Articles

Related Articles

Improve Article

Ruby | Array one?() function

  • Last Updated : 04 Dec, 2019

Array#one?() : one?() is a Array class method which checks whether the array is having only one array element.

Syntax: Array.one?()

Parameter: Array

Return: true – if array has only one element otherwise return false.

Example #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"

Output :

one?() method form : false

one?() method form : false

one?() method form : true

Example #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"

Output :

one?() method form : false

one?() method form : false

one?() method form : true




My Personal Notes arrow_drop_up
Recommended Articles
Page :