Module: IceCube::StringBuilder::Helpers
- Included in:
- IceCube::StringBuilder
- Defined in:
- lib/ice_cube/builders/string_builder.rb
Constant Summary collapse
- NUMBER_SUFFIX =
['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th']
- SPECIAL_SUFFIX =
{ 11 => 'th', 12 => 'th', 13 => 'th', 14 => 'th' }
Instance Method Summary collapse
- #nice_number(number) ⇒ Object
-
#sentence(array) ⇒ Object
influenced by ActiveSupport’s to_sentence.
Instance Method Details
#nice_number(number) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/ice_cube/builders/string_builder.rb', line 50 def nice_number(number) return 'last' if number == -1 suffix = SPECIAL_SUFFIX[number] || NUMBER_SUFFIX[number.abs % 10] if number < -1 number.abs.to_s << suffix << ' to last' else number.to_s << suffix end end |
#sentence(array) ⇒ Object
influenced by ActiveSupport’s to_sentence
41 42 43 44 45 46 47 48 |
# File 'lib/ice_cube/builders/string_builder.rb', line 41 def sentence(array) case array.length when 0 ; '' when 1 ; array[0].to_s when 2 ; "#{array[0]} and #{array[1]}" else ; "#{array[0...-1].join(', ')}, and #{array[-1]}" end end |