Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class CasePattern
def initialize(arr)
@arr = arr
end
def ===(other_arr)
@arr.zip(other_arr).all?{ |x1, x2| x1 === x2 }
end
module ConstructorContext
def self.___
CasePattern::Wildcard
end
end
class Wildcard
def self.===(x)
true
end
end
end
def Pattern(&block)
CasePattern.new(CasePattern::ConstructorContext.instance_eval(&block))
end
# Examples:
case [1, 'foobarbaz', 999]
when Pattern{[Integer, /foo/, 111]} then "I don't match"
when Pattern{[Integer, /foo/, ___]} then "I match!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.