Created
March 22, 2013 11:27
-
-
Save timrandg/5220597 to your computer and use it in GitHub Desktop.
Implementation wildcard operator for pattern-matching-like case statements in RUBY
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
#Here is a quick implementation of pattern matching for cases in ruby | |
module WildcardFunctionality | |
class Wildcard | |
def ==(other) | |
true | |
end | |
end | |
def _ | |
Wildcard.new | |
end | |
end | |
class Object | |
include WildcardFunctionality | |
end | |
#class MyClass | |
# def test_a_case_statement | |
# digital_data = [1,1,1] | |
# | |
# case digital_data | |
# when [0,1,1] then puts "match" | |
# when [_,1,1] then puts "match 2" | |
# else puts "couldn't find match" | |
# end | |
# end | |
# def test_another_case_statement | |
# first_name = "Andrew" | |
# last_name = "Huxley" | |
# | |
# case [first_name,last_name] | |
# when _ then puts "found the name" | |
# else puts "didn't find the name" | |
# end | |
# end | |
#end | |
# | |
#my_class = MyClass.new | |
#my_class.test_a_case_statement | |
#my_class.test_another_case_statement | |
# | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A detailed explanation of wildcard pattern matching and general globbing can be found here:
https://www.partow.net/programming/wildcardmatching/index.html