I’m looking for a way to parse ordinal numbers (first, second, third, etc) in Ruby and convert them to integers. Do you know of any libraries that do this?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I was half-way through asking this question when I realized that the
chronicgem does this as part of the process of parsing dates. After installing the gem, you can convert ordinal numbers to integers pretty easily:Edit: Unfortunately, it seems that
chronicdoesn’t correctly parse the ordinal “second”:The reason for this is that
chronicis designed to parse dates and times, and “second” could be either an ordinal number or a unit of time in that context. To solve this problem, you can monkey patchchronic‘sNumerizerclass with this line:Now it works:
If you are actually using
chronicfor its intended purpose though, you probably won’t want to screw with its internals. In that case, you can copy the source code fromChronic::Numerizerinto a new class and use that one instead. Don’t forget to add['second', '2']to theORDINALSconstant in the new class.