Skip to content

Instantly share code, notes, and snippets.

@rringler
Last active September 19, 2016 17:11
Show Gist options
  • Save rringler/ac075eb541ac926d167a66d235fc2f8d to your computer and use it in GitHub Desktop.
Save rringler/ac075eb541ac926d167a66d235fc2f8d to your computer and use it in GitHub Desktop.
Case Insensitive String Matching
# 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)
# 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)
# 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