简体   繁体   English

如何在 Ruby 中将 1 转换为“第一”,将 2 转换为“第二”,依此类推?

[英]How to convert 1 to "first", 2 to "second", and so on, in Ruby?

Ruby 中是否有内置方法来支持这一点?

if you are in Rails, you can convert 1 to 1st , 2 to 2nd , and so on, using ordinalize .如果您在 Rails 中,您可以使用ordinalize1转换为1st22nd ,依此类推。

Example:例子:

1.ordinalize # => "1st"
2.ordinalize # => "2nd"
3.ordinalize # => "3rd"
...
9.ordinalize # => "9th"
...
1000.ordinalize # => "1000th"

And if you want commas in large numbers:如果你想要大量的逗号:

number_with_delimiter(1000, :delimiter => ',') + 1000.ordinal # => "1,000th"

in ruby you do not have this method but you can add your own in Integer class like this.在 ruby​​ 中,您没有此方法,但您可以像这样在 Integer 类中添加自己的方法。

class Integer
  def ordinalize
    case self%10
    when 1
      return "#{self}st"
    when 2
      return "#{self}nd"
    when 3
      return "#{self}rd"
    else
      return "#{self}th"
    end
  end
end


22.ordinalize #=> "22nd"

How about Linguistics ? 语言学怎么样? Its not built in though.虽然它不是内置的。 If you want built in , you have to set it up using hashes etc.. Seehere also for examples如果你想内置,你必须使用散列等设置它。参见这里的例子

I wanted an ordinalize method that has "first, second, third" rather than '1st, 2nd, 3rd' - so here's a little snippet that works up to 10 (and falls back to the Rails ordinalize if it can't find it).我想要一个具有“第一、第二、第三”而不是“第一、第二、第三”的 ordinalize 方法 - 所以这里有一个小片段,最多可以使用 10(如果找不到,则回退到 Rails ordinalize) .

class TextOrdinalize

  def initialize(value)
    @value = value
  end

  def text_ordinalize
    ordinalize_mapping[@value] || @value.ordinalize
  end

  private

  def ordinalize_mapping
    [nil, "first", "second", "third", "fourth", "fifth", "sixth", "seventh",
      "eighth", "ninth", "tenth" ]
  end

end

Here's how it works:这是它的工作原理:

TextOrdinalize.new(1).text_ordinalize #=> 'first'
TextOrdinalize.new(2).text_ordinalize #=> 'second'
TextOrdinalize.new(0).text_ordinalize #=> '0st'
TextOrdinalize.new(100).text_ordinalize #=> '100th'

Using humanize gem, is probably the easiest way.使用humanize gem,可能是最简单的方法。 But, yes, it is not built in, however it has only one dependency, so I think its a pretty good choice..但是,是的,它不是内置的,但是它只有一个依赖项,所以我认为它是一个不错的选择..

require 'humanize'

2.humanize  => "two"

if you are not in Rails you could do如果你不在 Rails 中,你可以这样做

def ordinalize(n)
  return "#{n}th" if (11..13).include?(n % 100)

  case n%10
  when 1; "#{n}st"
  when 2; "#{n}nd"
  when 3; "#{n}rd"
  else    "#{n}th"
  end
end

ordinalize 1
=> "1st"
ordinalize 2
=> "2nd"
ordinalize 11
=> "11th"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将此函数从 Ruby 转换为 JavaScript 使其看起来相同 - How to convert this function from Ruby to JavaScript so it will look the same Ruby将第一个数组中的元素与第二个数组进行匹配 - Ruby match elements from the first with second array Ruby中的正则表达式匹配字符串的第一和第二个空格 - Regex in Ruby to match first and second space of strings Ruby:将gsub应用于第一和第二个数组项 - Ruby: Apply gsub to first and second array items Ruby:第二​​段与第一段相似的代码怎么会出现“未定义方法”错误? - Ruby: How come second piece of code, which is similar to first one - gives 'undefined method' error? 如何在ruby中获取第一个html标记和第二个html标记之间的内容 - how to get the content between the first html tag and the second html tag in ruby 如何将数组的第二个字符串添加到同一数组的第一个字符串中,ruby - How can I add the second string of an array to the first string of the same array, ruby 如何转换此group_by语句,使其在DB而不是Ruby上正常工作? - How can I convert this group_by statement so that it works off the DB instead of Ruby? 如何转换 Ruby 散列,使其所有键都是符号? - How do I convert a Ruby hash so that all of its keys are symbols? 如何转换Ruby哈希,使其所有键均为字符串 - How do I convert a Ruby hash so that all of its keys are strings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM