Module: HumanCodes::String

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

Instance Method Summary collapse

Instance Method Details

#human_code(options = {}) ⇒ Object



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

def human_code(options = {})
  return upcase.human_code if options[:fix_case]
  return self unless is_human_code?
  val = 0
  key = reverse
  (0...key.size).each do |i|
    char = key[i]
    integer = SAFE_CHARACTERS_AS_ASCII[char]
    val = val + (integer * (SAFE_BASE ** i))
  end
  val
end

#is_human_code?Boolean

Returns:

  • (Boolean)


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

def is_human_code?
  return false if nil?
  string = self.dup
  return false if string.length <= 0
  (0...string.size).each do |i|
    return false unless SAFE_CHARACTERS_AS_ASCII[string[i]]
  end
  true
end