Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialVineet Kapoor
3,955 Points"a" and "b" are strings. What does it mean to check which one is greater? Question not CLEAR!
Question is not clear. What shall be the answer "a" > "b" OR a = 1 b = 2 a > b
"a" > "b"
1 Answer
Kevin Korte
28,149 PointsRemember that there is an value associate with these strings. It would be that characters ANSI character set, so what you're really comparing is the numerical value, of the character set.
You can find these values here: http://www.alanwood.net/demos/ansi.html
Based on that we can see that "a" equals 97, and "b"equals 98, so what you are really evaluating is whether 97 is greater than 98. That's probably more human friendly to see. But it's good to know how the computer evaluates this under the hood. Also notice capital letters have different values than lowercase.
If you want to play with this, start a Ruby workspace, and than in the console, start up irb by typing irb
. Than you can type "a".ord
and it will return 97. You can type "b".ord
and it will return 98. Now you know how the computer reads it. So if you typed "a" < "b"
it would return true, since 97 < 98. You can go the the other way by typing in a number and .chr
. For instance, 55.chr
returns the string "7" from Ruby. Which still matches the ANSI form I linked above.
That's what is happening here.