Ruby / Rails中的整数序数化 [英] Integer ordinalization in Ruby/Rails

查看:161
本文介绍了Ruby / Rails中的整数序数化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在Ruby / Rails中处理整数序数化的方法,即。 st,nd,rd和th后缀为整数。 Ruby on Rails用于使用ordinalize方法扩展FixNum,但该功能似乎在版本3中已被弃用。

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.

我目前正在使用源代码旧的Rails方法,这是很好的...但这似乎是大多数脚本语言/ Web框架将内置的功能,我觉得Rails背后的人必须有一个原因,弃用功能(也许现在可用在Ruby中?)。

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?).

请指教!

推荐答案

a href =http://guides.rubyonrails.org/active_support_core_extensions.html#ordinalize =noreferrer> ordinalize 。

The method you want is still ordinalize.

Active_Support 被重构了一点,以提供更好的粒度。而不是一次性加载所有内容,您可以根据需要选择较小的块。

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.

您可以使用 require'加载Active_Support中的所有内容, active_support / all',或使用
require'active_support / core_ext / integer / inflections'分解它

>> require 'active_support/core_ext/integer/inflections' #=> true
>> 1.ordinalize #=> "1st"

这篇关于Ruby / Rails中的整数序数化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆