How to compare strings ignoring the case

Steven picture Steven · May 16, 2010 · Viewed 87.2k times · Source

I want apple and Apple comparison to be true. Currently

"Apple" == "Apple"  # returns TRUE
"Apple" == "APPLE"  # returns FALSE

Answer

molf picture molf · May 16, 2010

You're looking for casecmp. It returns 0 if two strings are equal, case-insensitively.

str1.casecmp(str2) == 0

"Apple".casecmp("APPLE") == 0
#=> true

Alternatively, you can convert both strings to lower case (str.downcase) and compare for equality.