Class: EasyPin::BaseConverter
- Inherits:
-
Object
- Object
- EasyPin::BaseConverter
- Defined in:
- lib/easy_pin.rb
Instance Method Summary collapse
- #convert(integer) ⇒ Object
-
#initialize(base) ⇒ BaseConverter
constructor
A new instance of BaseConverter.
- #unconvert(parts) ⇒ Object
Constructor Details
#initialize(base) ⇒ BaseConverter
Returns a new instance of BaseConverter.
63 64 65 |
# File 'lib/easy_pin.rb', line 63 def initialize(base) @base = base end |
Instance Method Details
#convert(integer) ⇒ Object
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/easy_pin.rb', line 67 def convert(integer) parts = [] while integer > 0 parts.unshift(integer % @base) integer = integer / @base end parts end |
#unconvert(parts) ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/easy_pin.rb', line 78 def unconvert(parts) sum = 0 parts.reverse.each_with_index do |part, index| sum += part * (@base ** index) end sum end |