Module: Alphadecimal::String

Included in:
String
Defined in:
lib/alphadecimal.rb

Instance Method Summary collapse

Instance Method Details

#alphadecimalObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/alphadecimal.rb', line 28

def alphadecimal
  return self unless is_alphadecimal?
  val = 0
  key = reverse
  key.bytes.each_with_index do |char, i|
     case
     when (B62_0..B62_9).include?(char) then norm = char - B62_0
     when (B62_A..B62_Z).include?(char) then norm = char - B62_A + 10
     when (B62_a..B62_z).include?(char) then norm = char - B62_a + 36
     end
    val = val + (norm * (62 ** i))
  end
  val
end

#is_alphadecimal?Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
# File 'lib/alphadecimal.rb', line 43

def is_alphadecimal?
  return false if nil?
  string = dup
  return false if string.length <= 0
  string.bytes.to_a.each do |b|
    return false unless B62_CHRS.include?(b)
  end
  true
end