Module: Alnum

Defined in:
lib/alnum.rb,
lib/alnum/symbols.rb,
lib/alnum/version.rb

Constant Summary collapse

BASE =
SYMBOLS.length
SYMBOLS =
%w{
  0 1 2 3 4 5 6 7 8 9
  a b c d e f g h i j k l m n o p q r s t u v w x y z
  A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
}
VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.cypher(number) ⇒ Object

Raises:

  • (TypeError)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/alnum.rb', line 7

def self.cypher(number)
  raise TypeError, "Only integers allowed" unless number.is_a? Integer   

  numbers = []

  until number < BASE do
    remainder = number % BASE
    numbers << SYMBOLS[remainder]
    number = (number - remainder) / BASE
  end

  numbers << SYMBOLS[number]
  numbers.reverse.join
end

.decipher(alphanumeric) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/alnum.rb', line 22

def self.decipher(alphanumeric)
  number, count = 0, 0

  alphanumeric.to_s.reverse.each_char() do |c|
    raise RangeError, "Code contains characters out of range: '#{c}'" if SYMBOLS.index(c).nil?
    number += SYMBOLS.index(c) * BASE**count
    count += 1
  end

  number    
end