I want to test 2 strings for equality in Ruby in a case insensitive manner.
In languages, such as Fantom, you simply write:
string1.equalsIgnoreCase(string2)
What's the idiomatic way to do this in Ruby?
You can use casecmp
"Test".casecmp("teST")
=> 0
"Test".casecmp("teST2")
=> -1
So to test for equality, you can do:
if str.casecmp(str2).zero?
# strings are equal
end
Though there is casecmp
:
0 == s1.casecmp(s2) # strings equal
I personally prefer
s1.downcase == s2.downcase
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With