Skip to content

Instantly share code, notes, and snippets.

@timrandg
Created March 22, 2013 11:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timrandg/5220597 to your computer and use it in GitHub Desktop.
Save timrandg/5220597 to your computer and use it in GitHub Desktop.
Implementation wildcard operator for pattern-matching-like case statements in RUBY
#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
#
@ArashPartow
Copy link

A detailed explanation of wildcard pattern matching and general globbing can be found here:

https://www.partow.net/programming/wildcardmatching/index.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment