One Thing
require first_and_only
[:thing].first_and_only! # => :thing
Too Many Things
require first_and_only
[1,2].first_and_only!
# => Enumerable::FirstAndOnly::LengthNotOne: 2
Not Enough Things
require first_and_only
[].first_and_only!
# => Enumerable::FirstAndOnly::LengthNotOne: 0
Works With ActiveRecord
require first_and_only
MyModel.count # => 0
MyModel.where(some: "query").first_and_only!
# => Enumerable::FirstAndOnly::LengthNotOne: 0
The eigen class strikes again: https://www.ruby-forum.com/topic/109820. That explains why I couldn't get it to work.