Rails has a nice function, ordinalize, which converts an integer to a friendly string representation. Namely 1 becomes 1st, 2 becomes 2nd, and so on. My question is how might one implement the inverse feature?
To be more general I’d like to handle both of the following cases:
>> s = 'First' >> s.integerize => 1 >> s = 1st >> s.integerize => 1
I am looking for an smart way to do this as opposed to a giant lookup table or just hacking off the last two characters. Any ideas would be appreciated.
to_i does essentially 1/2 of that:
It doesn’t check validity, but if you need to fail on bad input like ’72x’, you can just re-ordinalize and compare to the original input string.
For parsing ordinal words, Wikipedia seems impressively helpful.