module Base62
Constants
- BASE
- KEYS
- KEYS_HASH
- VERSION
Public Class Methods
decode(str)
click to toggle source
Decodes base62 string to a base10 (decimal) number.
# File lib/base62-rb.rb, line 23 def self.decode(str) num = 0 i = 0 len = str.length - 1 # while loop is faster than each_char or other 'idiomatic' way while i < str.length pow = BASE**(len - i) num += KEYS_HASH[str[i]] * pow i += 1 end num end
encode(num)
click to toggle source
Encodes base10 (decimal) number to base62 string.
# File lib/base62-rb.rb, line 9 def self.encode(num) return "0" if num == 0 return nil if num < 0 str = "" while num > 0 # prepend base62 charaters str = KEYS[num % BASE] + str num = num / BASE end str end