id:Naruhodiusさんに倣って僕もやってみました:)

class Integer
  def ordinalize
    suffix =
      if (fd=abs%10).between?(1,3) && !abs.between?(11,13)
        %w(_ st nd rd)[fd]
      else
        'th'
      end
    "#{self}" + suffix
  end
end
(-25..25).map(&:ordinalize) # => ["-25th", "-24th", "-23rd", "-22nd", "-21st", "-20th", "-19th", "-18th", "-17th", "-16th", "-15th", "-14th", "-13th", "-12th", "-11th", "-10th", "-9th", "-8th", "-7th", "-6th", "-5th", "-4th", "-3rd", "-2nd", "-1st", "0th", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th", "21st", "22nd", "23rd", "24th", "25th"]

ActiveSupport::Inflector::Inflectionsに、ordinalizeメソッドというのがあるみたいですね。

ordinalize



blog comments powered by Disqus
ruby_pack8

100円〜で好評発売中!
M'ELBORNE BOOKS