Class: BaseConvert::Number

Inherits:
Object
  • Object
show all
Includes:
Methods
Defined in:
lib/base_convert/number.rb

Direct Known Subclasses

Entropia

Constant Summary collapse

INDEXa =
DIGITS[:P95].index('a')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Methods

#chars_ordered?, #toi, #tos

Constructor Details

#initialize(counter = 0, base: nil, digits: nil, validate: true) ⇒ Number

Returns a new instance of Number.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/base_convert/number.rb', line 29

def initialize(counter=0, base: nil, digits: nil, validate: true)
  @validate = !!validate # ensure boolean

  # counter
  string = nil
  case counter
  when String
    string = counter
    if base.nil? and digits.nil?
      base,digits = Number.infer(counter, error:'need digits to cover string')
    end
  when Integer
    @integer = counter
    base, digits = 10, DIGITS[:P95]  if base.nil? and digits.nil?
  else
    raise 'need counter String|Integer'
  end

  # digits
  @digits = DIGITS[digits || base]

  # base
  base = digits  if base.nil? and digits.is_a? Symbol
  @base = BASE[base || @digits.length]

  # validate
  if @validate
    raise 'digits must cover base'  if @base > @digits.length
    unless string.nil? or string.empty?
      indeces = string.chars.map{@digits.index _1}
      if missing=indeces.any?{_1.nil?} or exceeding=indeces.any?{_1>=@base}
        if @base <= INDEXa and DIGITS[:P95].start_with?(@digits)
          # User ignored case, so fix it...
          string = string.upcase
          indeces = string.chars.map{@digits.index _1}
          missing=indeces.any?{_1.nil?} or exceeding=indeces.any?{_1>=@base}
        end
        raise 'digits must cover string'  if missing
        raise 'digits in string must be under base'  if exceeding
      end
    end
    unless @integer.nil?
      raise 'integer can not be negative'  if @integer < 0
    end
  end

  @integer = toi(string)  if @integer.nil?
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



28
29
30
# File 'lib/base_convert/number.rb', line 28

def base
  @base
end

#digitsObject (readonly)

Returns the value of attribute digits.



28
29
30
# File 'lib/base_convert/number.rb', line 28

def digits
  @digits
end

Class Method Details

.infer(string, error: 'digits not all in P95') ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/base_convert/number.rb', line 7

def self.infer(string, error:'digits not all in P95')
  p95 = DIGITS[:P95]
  return 2, p95  if string.empty?
  chars = string.chars
  raise error unless chars.all?{|_|p95.include?_}
  max = chars.map{|_|p95.index(_)}.max
  return 95, p95  if max == 94 # string has a space digit.
  return 2,  p95  if max < 2
  return 4,  p95  if max < 4
  return 8,  p95  if max < 8
  return 10, p95  if max < 10
  return 16, p95  if max < 16
  return 32, p95  if max < 32
  u47 = DIGITS[:U47]
  return 47, u47  if chars.all?{|_|u47.include?_}
  return 64, p95  if max < 64
  b64 = DIGITS[:B64]
  return 64, b64  if chars.all?{|_|b64.include?_}
  return 94, p95
end

Instance Method Details

#inspectObject



78
79
80
81
# File 'lib/base_convert/number.rb', line 78

def inspect
  d = DIGITS.label(@digits)
  "#{to_s} #{@base}:#{d}"
end

#to_base(base, digits = (base.is_a?Symbol)? DIGITS[base] : @digits, validate = @validate) ⇒ Object



87
88
89
90
91
# File 'lib/base_convert/number.rb', line 87

def to_base(base,
            digits   = (base.is_a?Symbol)? DIGITS[base] : @digits,
            validate = @validate)
  Number.new @integer, base: base, digits: digits, validate: validate
end

#to_digits(digits, base = @base, validate = @validate) ⇒ Object



93
94
95
# File 'lib/base_convert/number.rb', line 93

def to_digits(digits, base=@base, validate=@validate)
  Number.new @integer, base: base, digits: digits, validate: validate
end

#to_iObject



85
# File 'lib/base_convert/number.rb', line 85

def to_i = @integer

#validate?Boolean

Returns:

  • (Boolean)


83
# File 'lib/base_convert/number.rb', line 83

def validate? = @validate