Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/to_insane.rb

Instance Method Summary collapse

Instance Method Details

#from_insane(base_val = nil, alpha = nil) ⇒ Object

Converts a string to a base x integer using y character set.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/to_insane.rb', line 9

def from_insane(base_val = nil, alpha = nil)
  base_val ||= 58
  alpha ||= INSANECHARACTERS
  base_val = alpha.size if base_val == :max
  if base_val == :url_safe
    base_val = 63
    alpha = INSANECHARACTERS
  end
  raise ArgumentError, "Base Value is not valid. Integer, :max, :url_safe" unless base_val.kind_of? Integer
  raise ArgumentError, "Base size is too large for given character set. Please use a Base value larger than 1 and less than #{alpha.length}" if base_val > alpha.length or base_val <= 1    
  raise ArgumentError, "Character set needs to be larger than 0" if alpha.size <= 0
  raise ArgumentError, "Character set has duplicate characters" if alpha.size != alpha.split(//).uniq.size
  int_val = 0
  char_ref = alpha[0..base_val]
  self.reverse.split(//).each_with_index do |char,index|
    raise ArgumentError, "Character #{char} at index #{index} is not a valid character for to_insane Base #{base_val} String." unless char_index = char_ref.index(char)
    int_val += (char_index)*(base_val**(index))
  end
  int_val
end