Class: BaseConvert::FromTo
- Inherits:
-
Object
- Object
- BaseConvert::FromTo
- Includes:
- Methods
- Defined in:
- lib/base_convert/from_to.rb
Instance Method Summary collapse
- #convert(counter) ⇒ Object (also: #[])
-
#initialize(base: 10, to_base: base, digits: :P95, to_digits: digits) ⇒ FromTo
constructor
A new instance of FromTo.
- #inspect ⇒ Object
Methods included from Methods
Constructor Details
#initialize(base: 10, to_base: base, digits: :P95, to_digits: digits) ⇒ FromTo
Returns a new instance of FromTo.
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/base_convert/from_to.rb', line 5 def initialize(base: 10, to_base: base, digits: :P95, to_digits: digits) base = BASE[base] to_base = BASE[to_base] digits = DIGITS[digits] to_digits = DIGITS[to_digits] if base > digits.length or to_base > to_digits.length raise 'base must cover digits' end @base, @to_base, @digits, @to_digits = base, to_base, digits, to_digits end |
Instance Method Details
#convert(counter) ⇒ Object Also known as: []
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/base_convert/from_to.rb', line 22 def convert(counter) case counter when Integer tos(counter, @to_base, @to_digits) when String tos(toi(counter), @to_base, @to_digits) else raise 'counter must be String|Integer' end end |