Rails has an Array#exclude? method
In my vendetta against the unless expression in ruby, I came across a use case where I wanted to execute code only if an item was missing from an array.
I could easily do:
unless ['a', 'b', 'c'].include?('d')
# do a thing
end
But I wanted to replace the unless with an if, which led me to wonder if there was an exclude? method for arrays in ruby.
Turns out, ActiveSupport extended the Enumerable class to introduce exactly what I was looking for!
if ['a', 'b', 'c'].exclude?('d')
# do a thing
end
Hooray!
Tweet