Created
December 17, 2018 19:00
-
-
Save havenwood/1017d387dc468d069a6060c79e3becd6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module SentinelEnumerable | |
refine Enumerable do | |
def sentinel_any? sentinel_value: :no_value | |
falsey = [sentinel_value, false].freeze | |
value_proc = if block_given? | |
-> value { yield value } | |
else | |
:itself.to_proc | |
end | |
each do |value| | |
return true unless falsey.include? value_proc.call(value) | |
end | |
false | |
end | |
end | |
end | |
using SentinelEnumerable | |
puts [:no_value].sentinel_any? &:to_sym | |
#>> false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment