Created
August 31, 2021 03:53
-
-
Save westonganger/5fc30de78f3495bd9cc386a7582dc28c to your computer and use it in GitHub Desktop.
Ruby Case-Insentive Strings
This file contains 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
################################################## CIString (Case-Insensitive String) - HELPFUL FOR STRING COMPARISONS WHERE WE NEED TO IGNORE CASE ERRORS | |
class CIString < String | |
def initialize(*args) | |
if args[0].nil? | |
args[0] = "" | |
end | |
super | |
end | |
def ==(val) | |
val.is_a?(String) ? self.casecmp?(val) : false | |
end | |
def ===(val) | |
val.is_a?(String) ? self.casecmp?(val) : false | |
end | |
end | |
String.class_eval do | |
prepend(CIStringFix = Module.new do | |
def ==(val) | |
val.is_a?(CIString) ? self.casecmp?(val) : super | |
end | |
def ===(val) | |
val.is_a?(CIString) ? self.casecmp?(val) : super | |
end | |
end) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment