Method: Array#only
- Defined in:
- lib/core/facets/array/only.rb
#only ⇒ Object
Returns the only element in the array. Raises an IndexError if the array’s size is not 1.
[5].only # => 5
expect IndexError do
[1,2,3].only
end
expect IndexError do
[].only
end
CREDIT: Gavin Sinclair, Noah Gibbs
18 19 20 21 22 23 |
# File 'lib/core/facets/array/only.rb', line 18 def only unless size == 1 raise IndexError, "Array#only called on non-single-element array" end first end |