Hy,
I am trying to understand how Ruby handles strings when comes to logical comparison.
I tried:
"a" > "b" #--> false
"ab" > "b" #--> false
"z" > "az" #--> true
# i even tried
"z" > "abcdefghijklmnopqrstuvwxyz" #--> true
Is like Ruby gives weight to characters from a(the weakest) to z(the strongest) and it doesn't even check furter from the first character only.
I would appreciate a explanation for this phenomenon. Thank you very much.
Strings are ordered as in a wordbook.
string1 > string2
is true ifstring1
appears later in the book thanstring2
."a" is on page 1, "b" is on page 30, so
"a">"b"
is false.It's the same reason that if you have a folder full of files with numbers in filenames,
10.txt
and11.txt
appear between1.txt
and2.txt
It's called lexicographical ordering, and it ignores the second letters if the first letters are different.