Open In App

Ruby | Array one?() function

Last Updated : 26 Dec, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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
# 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
# Ruby code for one?() method

# declaring array
a = ["abc", "nil", "dog"]

# declaring array
b = ["cow"]

# declaring array
c = ["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

Next Article
Article Tags :

Similar Reads