Class: BaseConvert::Number
- Inherits:
-
Object
- Object
- BaseConvert::Number
show all
- Includes:
- BaseConvert
- Defined in:
- lib/base_convert/number.rb
Constant Summary
Constants included
from BaseConvert
BASE, DIGITS, INDEXa, VERSION
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#ascii_ordered?, #toi, #tos
Constructor Details
#initialize(counter = 0, base: nil, digits: nil, validate: true) ⇒ Number
Returns a new instance of Number.
28
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
77
78
|
# File 'lib/base_convert/number.rb', line 28
def initialize(counter=0, base: nil, digits: nil, validate: true)
case validate
when true, false
@validate = validate
else
raise 'validate must be either true or false'
end
string = nil
case counter
when String
string = counter
base, digits = Number.infer(counter) if base.nil? and digits.nil?
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 || base]
base = digits if base.nil? and digits.is_a? Symbol
@base = BASE[base || @digits.length]
if @validate
raise 'digits must cover base' if @base > @digits.length
unless string.nil? or string.empty?
indeces = string.chars.map{|_|@digits.index(_)}
if missing = indeces.any?{|_|_.nil?} or exceeding = indeces.any?{|_|_>=@base}
if @base <= INDEXa and DIGITS[:P95].start_with?(@digits)
string = string.upcase
indeces = string.chars.map{|_|@digits.index(_)}
missing = indeces.any?{|_|_.nil?} or exceeding = indeces.any?{|_|_>=@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
#base ⇒ Object
Returns the value of attribute base.
27
28
29
|
# File 'lib/base_convert/number.rb', line 27
def base
@base
end
|
#digits ⇒ Object
Returns the value of attribute digits.
27
28
29
|
# File 'lib/base_convert/number.rb', line 27
def digits
@digits
end
|
Class Method Details
.infer(string) ⇒ Object
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/base_convert/number.rb', line 6
def self.infer(string)
p95 = DIGITS[:P95]
return 2, p95 if string.empty?
chars = string.chars
raise 'need digits to cover string' unless chars.all?{|_|p95.include?_}
max = chars.map{|_|p95.index(_)}.max
return 95, p95 if max == 94 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
#inspect ⇒ Object
80
81
82
83
|
# File 'lib/base_convert/number.rb', line 80
def inspect
d = DIGITS.label(@digits)
"#{to_s} #{@base}:#{d}"
end
|
#to_base(base, digits = @digits, validate = @validate) ⇒ Object
95
96
97
|
# File 'lib/base_convert/number.rb', line 95
def to_base(base, digits=@digits, validate=@validate)
Number.new @integer, base: base, digits: digits, validate: validate
end
|
#to_digits(digits, base = @base, validate = @validate) ⇒ Object
99
100
101
|
# File 'lib/base_convert/number.rb', line 99
def to_digits(digits, base=@base, validate=@validate)
Number.new @integer, base: base, digits: digits, validate: validate
end
|
#to_i ⇒ Object
91
92
93
|
# File 'lib/base_convert/number.rb', line 91
def to_i
@integer
end
|
#validate? ⇒ Boolean
85
86
87
|
# File 'lib/base_convert/number.rb', line 85
def validate?
@validate
end
|