Created
September 22, 2018 18:16
-
-
Save markprzepiora/d05ee55908b2d4ea59d901ab7a0c52fb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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