In Files

Parent

Methods

Base36

Class containing methods to encode and decode Base 36. Note that the ALPH is [alph + num] and not [num + alph] which prevents us from simply using .to_i(36) and .to_s(36).

en.wikipedia.org/wiki/Base_36

Public Class Methods

decode(base36) click to toggle source

Method that decodes a base36 string and returns an integer.

    # File base36.rb, line 55
55:   def self.decode(base36)
56:     raise Base36Error if base36.empty?
57: 
58:     result = 0
59:     pos    = 0
60: 
61:     base36.upcase.reverse.each_char do |char|
62:       result += ALPH.index(char) * (BASE36 ** pos)
63:       pos    += 1
64:     end
65: 
66:     return result;
67:   end
encode(num) click to toggle source

Method that encodes an integer into a base36 string that is returned.

    # File base36.rb, line 39
39:   def self.encode(num)
40:     raise Base36Error unless num.is_a? Fixnum
41: 
42:     base36 = ""
43: 
44:     while num > 0
45:       base36 << ALPH[(num % BASE36)]
46:       num /= BASE36
47:     end
48: 
49:     base36 << ALPH[0] if num == 0
50: 
51:     base36.reverse
52:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.