Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer ordinalization in Ruby/Rails

I'm looking for a way to handle integer ordinalization in Ruby/Rails, ie. "st", "nd", "rd", and "th" suffixes to integers. Ruby on Rails used to extend FixNum with an "ordinalize" method, but that functionality seems to have been deprecated in version 3.

I am currently just using the source for the old Rails method, which is fine... but this seems like functionality that most scripting languages / web frameworks would have built in, and I feel like the folks behind Rails must have had a reason for deprecating the functionality (perhaps it is now available in Ruby proper?).

Please advise!

like image 268
Ashoat Avatar asked Dec 27 '10 05:12

Ashoat


1 Answers

The method you want is still ordinalize.

Active_Support was refactored a bit to provide better granularity. Instead of loading in everything at once, you can select smaller chunks depending on what you need.

You can either load everything in Active_Support using require 'active_support/all', or break it down using require 'active_support/core_ext/integer/inflections':

>> require 'active_support/core_ext/integer/inflections' #=> true
>> 1.ordinalize #=> "1st"
like image 197
the Tin Man Avatar answered Nov 17 '22 09:11

the Tin Man