Last active
September 19, 2016 17:11
-
-
Save rringler/ac075eb541ac926d167a66d235fc2f8d to your computer and use it in GitHub Desktop.
Case Insensitive String Matching
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
# Create a new predicate method | |
# | |
# Eg., 'ABC'.similar?('abc') # true | |
# | |
# Pros: Simple. Intention-revealing. | |
# Cons: No sugar. | |
module CoreExtensions | |
module String | |
def similar?(value) | |
casecmp(value).zero? | |
end | |
end | |
end | |
String.prepend(CoreExtensions::String) |
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
# Create a new operator by combining a binary & unary operator | |
# | |
# Eg., 'ABC' ==~ 'abc' && 'ABC' !=~ 'def' # true | |
# | |
# Pros: Syntactic sugar. Allows overloading the == and != operators. | |
# Cons: Rubocop complains about the lack of space between operators. | |
module CoreExtensions | |
module String | |
def ~ | |
@complemented = !@complemented | |
end | |
def complemented? | |
@complemented | |
end | |
def ==(value) | |
if value.respond_to?(:complemented?) && value.complemented? | |
~value # uncomplement value | |
casecmp(value).zero? | |
else | |
super | |
end | |
end | |
def !=(value) | |
if value.respond_to?(:complemented?) && value.complemented? | |
~value # uncomplement value | |
!casecmp(value).zero? | |
else | |
super | |
end | |
end | |
end | |
end | |
String.prepend(CoreExtensions::String) |
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
# Overload an existing operator | |
# | |
# Eg., 'ABC' =~ 'abc' # true | |
# | |
# Pros: Syntactic sugar. Stylistically correct according to Rubocop. | |
# Cons: Could be unintuitive to rubyists unaware of our core extensions. | |
# Unable to define !=~. | |
module CoreExtensions | |
module String | |
def =~(value) | |
if value.is_a?(String) | |
casecmp(value).zero? | |
else | |
super | |
end | |
end | |
end | |
end | |
String.prepend(CoreExtensions::String) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment