问题
What would be the right way to find the location of a character within the alphabet? For example:
"A".find_score # => 1
"C".find_score # => 3
回答1:
"A".ord
returns 65, the numeric code for "A", which is what the alphabet starts at. If you want it to start at 1 you could just subtract 64:
def get_code(c)
c.upcase.ord - 'A'.ord + 1
end
which works like:
get_code('A') # 1
get_code('B') # 2
get_code('C') # 3
来源:https://stackoverflow.com/questions/33747126/find-a-letters-alphabetical-score