Skip to content

Instantly share code, notes, and snippets.

@westonganger
Created August 31, 2021 03:53
Show Gist options
  • Save westonganger/5fc30de78f3495bd9cc386a7582dc28c to your computer and use it in GitHub Desktop.
Save westonganger/5fc30de78f3495bd9cc386a7582dc28c to your computer and use it in GitHub Desktop.
Ruby Case-Insentive Strings
################################################## 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