Array#one?

arr.one? { |element| block } -> true or false
Returns: boolean · Updated March 13, 2026 · Array Methods
arrays enumerable conditionals checking

one? is an Enumerable method that tests whether exactly one element in a collection matches a given condition. It returns true if precisely one element satisfies the criteria, otherwise false.

Syntax

array.one? { |element| block }

Without a block, it checks for truthy values.

Parameters

ParameterTypeDefaultDescription
blockProcA condition to evaluate against each element

Examples

Basic usage

numbers = [1, 2, 3, 4, 5]

numbers.one? { |n| n == 3 }
# => true (exactly one element equals 3)

numbers.one? { |n| n > 4 }
# => false (two elements: 4 and 5)

Without a block

[0, 1, 2].one?
# => true (exactly one truthy value: 1)

[false, nil, 1].one?
# => true

[0, false, nil].one?
# => false (no truthy values)

With pattern matching (Ruby 3.0+)

items = [1, "hello", :symbol, 3.14]

items.one?(String)
# => true (only one String element)

items.one?(Integer)
# => false (multiple integers)

Errors

Empty arrays always return false:

[].one?
# => false

Performance Notes

one? stops after finding the second match, making it efficient for large datasets when you only need to know if there is exactly one match.

See Also