Comparing Strings in Ruby
Ruby strings can be compared using comparison operators (==
, <
, >
, etc.).
"hello" == "hello" # true
"hello" == "goodbye" # false
String comparison in Ruby is case sensitive:
"hi" == "HI" # false
"oRanGE" == "ORanGe" # false
"oRanGE".downcase == "ORanGe".downcase # true
The comparisons where you just check equality are fairly straight forward. It gets more interesting with >
and <
comparisons. Here are some examples:
"cat" > "car" # true
"cat" < "car" # false
"cat" > "dog" # false
"Cat" > "dog" # false
"cat" > "Dog" # true