Helper to print numbers as ordinals (1st, 2nd, 3rd...)
1 2 def number_to_ordinal(num) 3 num = num.to_i 4 if (10...20)===num 5 "#{num}th" 6 else 7 g = %w{ th st nd rd th th th th th th } 8 a = num.to_s 9 c=a[-1..-1].to_i 10 a + g[c] 11 end 12 end 13 14 number_to_ordinal(12) => "12th" 15 number_to_ordinal(7) => "7th"
Note that floats will be converted to ints (without rounding) before processing.