module Base62

Constants

PRIMITIVES
VERSION

Public Class Methods

decode(str) click to toggle source
# File lib/base62.rb, line 9
def decode(str)
  out = 0
  str.split(//).reverse.each_with_index do |char, index|
    place = PRIMITIVES.size ** index
    out += PRIMITIVES.index(char) * place
  end
  out
end
encode(int) click to toggle source
# File lib/base62.rb, line 18
def encode(int)
  return "0" if int == 0

  rem = int
  result = ''
  while rem != 0
    result = PRIMITIVES[rem % PRIMITIVES.size].to_s + result
    rem /= PRIMITIVES.size
  end
  result
end