Module: Alphadecimal::String

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

Instance Method Summary collapse

Instance Method Details

#alphadecimalObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/alphadecimal.rb', line 32

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)


47
48
49
50
51
52
53
54
55
# File 'lib/alphadecimal.rb', line 47

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