Module: BASE_CONVERT::HELPERS

Includes:
CONFIG
Included in:
BaseConvert, Number
Defined in:
lib/base_convert/helpers.rb

Constant Summary

Constants included from CONFIG

CONFIG::BASE, CONFIG::DIGITS, CONFIG::INDEXa, CONFIG::QGRAPH, CONFIG::WORD

Instance Method Summary collapse

Instance Method Details

#base(base) ⇒ Object



32
33
34
# File 'lib/base_convert/helpers.rb', line 32

def base(base)
  (base.class == Symbol)? BASE[base]: base
end

#digits(base) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/base_convert/helpers.rb', line 22

def digits(base)
  if base.class == Symbol
    if digits = DIGITS[base]
      return digits
    end
    base = BASE[base]
  end
  (base > WORD.length)? QGRAPH : WORD
end

#upcase?(base, digits) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/base_convert/helpers.rb', line 6

def upcase?(base, digits)
  base <= INDEXa and digits.equal?(WORD)
end

#validate(base, digits) ⇒ Object



10
11
12
13
14
# File 'lib/base_convert/helpers.rb', line 10

def validate(base, digits)
  raise 'base is not an integer' unless base.kind_of?(Integer)
  raise 'digits not an Array'    unless digits.kind_of?(Array)
  raise 'base not between 2 and digits.length' unless base.between?(2, digits.length)
end

#validate_string(string, base, digits) ⇒ Object



16
17
18
19
20
# File 'lib/base_convert/helpers.rb', line 16

def validate_string(string, base, digits)
  string.chars.uniq.each do |c|
    raise 'String has invalid character' unless (i=digits.find_index(c)) and (i<base)
  end
end