Method: Fear::PatternMatchingApi#case

Defined in:
lib/fear/pattern_matching_api.rb

#case(*guards, &function) ⇒ Fear::PartialFunction

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

to make more complex matches, you are encouraged to use Qo gem.

Creates partial function defined on domain described with guards

Examples:

pf = Fear.case(Integer) { |x| x / 2 }
pf.defined_at?(4) #=> true
pf.defined_at?('Foo') #=> false

multiple guards combined using logical “and”

pf = Fear.case(Integer, :even?.to_proc) { |x| x / 2 }
pf.defined_at?(4) #=> true
pf.defined_at?(3) #=> false
Fear.case(Qo[age: 20..30]) { |_| 'old enough' }

Parameters:

  • guards (<#===>)
  • function (Proc)

Returns:

See Also:

  • https://github.com/baweaver/qo
[View source] [View on GitHub]

105
106
107
# File 'lib/fear/pattern_matching_api.rb', line 105

def case(*guards, &function)
  PartialFunction.and(*guards, &function)
end