Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created December 17, 2018 19:00
Show Gist options
  • Save havenwood/1017d387dc468d069a6060c79e3becd6 to your computer and use it in GitHub Desktop.
Save havenwood/1017d387dc468d069a6060c79e3becd6 to your computer and use it in GitHub Desktop.
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