Class: DECRadix50::Decoder

Inherits:
Object
  • Object
show all
Defined in:
lib/dec_radix_50/decoder.rb

Overview

Encodes strings to the DEC Radix-50 encoding.

Instance Method Summary collapse

Constructor Details

#initialize(charset, encoded_values) ⇒ Decoder

Returns a new instance of Decoder.



8
9
10
11
12
13
14
# File 'lib/dec_radix_50/decoder.rb', line 8

def initialize(charset, encoded_values)
  @charset = charset
  @encoded_values = encoded_values
  @base = charset.length

  _validate_input_parameters
end

Instance Method Details

#decodeObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/dec_radix_50/decoder.rb', line 16

def decode
  charset_values = @charset.split(//).each_with_index.to_h.invert

  @encoded_values.map do |value|
    x = charset_values[(value / @base / @base) % @base]
    y = charset_values[(value / @base) % @base]
    z = charset_values[value % @base]
    "#{x}#{y}#{z}"
  end.join
end