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
AMBIGUOUS, BASE, BASE64, DIGITS, G94, GRAPH, INDEXa, QGRAPH, QUOTES, UNAMBIGUOUS, UNDERSCORE, VERSION, WORD, WORD_
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#tob, #toi
Constructor Details
#initialize(counter = 0, base: nil, digits: nil, validate: true) ⇒ Number
Returns a new instance of Number.
24
25
26
27
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
79
80
|
# File 'lib/base_convert/number.rb', line 24
def initialize(counter=0, base: nil, digits: nil, validate: true)
case validate
when true, false
@validate = validate
else
raise "validate must be either true of false."
end
case counter
when String
@string = counter
base, digits = Number.infer(counter) if base.nil? and digits.nil?
when Integer
@integer = counter
base, digits = 10, G94 if base.nil? and digits.nil?
else
raise "Need counter String|Integer."
end
if digits.is_a? Symbol
digits = DIGITS[digits]
raise "Unrecognized digits." if digits.nil?
end
digits = DIGITS[base] if digits.nil?
digits = G94 if digits.nil?
raise "digits must be a String of at least length 2." unless digits.is_a?(String) and digits.length > 1
@digits = digits
if base.is_a? Symbol
base = BASE[base]
raise "Unrecognized base." if base.nil?
end
base = @digits.length if base.nil?
raise "base must be an Integer greater than 1." unless base.is_a?(Integer) and base > 1
@base = base
if @validate
raise "digits must cover base." if @base > @digits.length
raise "digits must not have duplicates." if @digits.length > @digits.chars.uniq.length
unless @string.nil? or @string.empty?
@string.upcase! if @base <= INDEXa and @digits.equal? G94
raise "digits must cover string." unless @string.chars.all?{|_|@digits.include?_}
raise "digits in string must be under base." unless @base > @string.chars.map{|_|@digits.index(_)}.max
end
unless @integer.nil?
raise "integer can't be negative." if @integer < 0
end
end
@integer = toi if @integer.nil?
@string = tob if @string.nil?
end
|
Instance Attribute Details
#base ⇒ Object
Returns the value of attribute base.
23
24
25
|
# File 'lib/base_convert/number.rb', line 23
def base
@base
end
|
#digits ⇒ Object
Returns the value of attribute digits.
23
24
25
|
# File 'lib/base_convert/number.rb', line 23
def digits
@digits
end
|
Class Method Details
.infer(string) ⇒ Object
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/base_convert/number.rb', line 5
def self.infer(string)
return 2, G94 if string.empty?
chars = string.chars
raise 'Need digits.' unless chars.all?{|_|G94.include?_}
max = chars.map{|_|G94.index(_)}.max
return 2, G94 if max < 2
return 4, G94 if max < 4
return 8, G94 if max < 8
return 10, G94 if max < 10
return 16, G94 if max < 16
return 32, G94 if max < 32
[UNAMBIGUOUS, BASE64].each do |digits|
return digits.length, digits if chars.all?{|_|digits.include?_}
end
return 64, G94 if max < 64
return G94.length, G94 if max < 64
end
|
Instance Method Details
#to_base(base, digits = @digits, validate = @validate) ⇒ Object
94
95
96
|
# File 'lib/base_convert/number.rb', line 94
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
98
99
100
|
# File 'lib/base_convert/number.rb', line 98
def to_digits(digits, base=@base, validate=@validate)
Number.new @integer, base: base, digits: digits, validate: validate
end
|
#to_i ⇒ Object
90
91
92
|
# File 'lib/base_convert/number.rb', line 90
def to_i
@integer
end
|
#to_s ⇒ Object
86
87
88
|
# File 'lib/base_convert/number.rb', line 86
def to_s
@string
end
|
#validate? ⇒ Boolean
82
83
84
|
# File 'lib/base_convert/number.rb', line 82
def validate?
@validate
end
|