Module: Shorten::Numeric
Instance Method Summary collapse
-
#shorten(chars = Shorten::BASE62) ⇒ String
Shortened string.
Instance Method Details
#shorten(chars = Shorten::BASE62) ⇒ String
Returns Shortened string.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/shorten.rb', line 10 def shorten chars = Shorten::BASE62 raise ArgumentError.new('String required') unless chars.is_a? String raise ArgumentError.new('Only non-negative integers can be shortened') if self < 0 num = self len = chars.length str = '' while num > 0 mod = num % len str = chars[mod, 1] + str num /= len end str end |