Ruby中是否有内置方法将数字Year转换为字符串表示?
示例:"2015"
=> "twentyfifteen"
我知道我可以这样做:
def numeric_year_to_string(numeric_year)
years = {"2015" => "twentyfifteen", "2014" => "twentyfourteen" }
years.has_key?(numeric_year) ? years[numeric_year] : "twentyfifteen"
end
答案 0 :(得分:3)
Ruby中没有宝石或扩展程序可以执行此操作 - 您必须自己编写一个。
Humanize
最接近 - https://github.com/radar/humanize
require 'humanize'
要求'人性化'
def numeric_year_to_string(numeric_year)
tens = numeric_year%100
hundreds = (numeric_year - tens) / 100
return hundreds.humanize + " oh-" + tens.humanize if tens < 10
hundreds.humanize + " " + tens.humanize
end
试试这个 - 它适用于大多数情况