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
Totally agree, that's what I wanted to do... All the ways I tried to include a module into a module I kept getting
undefined method
when calling#first_and_only!
(including the way you pointed out)Good point.
That's what I get for going on memory. Better than "extending array", I've modified the implementation to take
#first(2)
in the size check. This should be a nice middle ground.