module Shorten::Numeric

Public Instance Methods

shorten(chars = Shorten::BASE62) click to toggle source

@param [String] chars Sequence of characters for shortening @return [String] Shortened string

# 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