class ToDecimal::Convertor
Ensure no other convertor object will be created
The main class that performs the actual conversion
Attributes
base[R]
Public Class Methods
new(base)
click to toggle source
# File lib/to_decimal/convertor_class.rb, line 6 def initialize(base) validate_base(base) @base = base freeze end
Public Instance Methods
[](input)
click to toggle source
# File lib/to_decimal/convertor_class.rb, line 12 def [](input) input = remove_leading_zeros_of(input) Validator.new(input, base).validate input_as_integer = integerize(input) convert(input_as_integer) end
Private Instance Methods
convert(integer)
click to toggle source
# File lib/to_decimal/convertor_class.rb, line 26 def convert(integer) integer.to_s.to_i(base) end
integerize(input)
click to toggle source
# File lib/to_decimal/convertor_class.rb, line 30 def integerize(input) input.is_a?(String) ? input.to_i : input end
remove_leading_zeros_of(input)
click to toggle source
# File lib/to_decimal/convertor_class.rb, line 22 def remove_leading_zeros_of(input) input.is_a?(String) ? input.sub(/\A0+/, '') : input end
validate_base(base)
click to toggle source
# File lib/to_decimal/convertor_class.rb, line 34 def validate_base(base) raise ArgumentError, 'Base must be an Integer' unless base.is_a?(Integer) raise ArgumentError, 'Base must be 1..10' unless \ base.positive? && base <= 10 end