Class: DECRadix50::Encoder

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

Overview

Encodes strings to the DEC Radix-50 encoding.

Instance Method Summary collapse

Constructor Details

#initialize(charset, string, replace_char) ⇒ Encoder

Returns a new instance of Encoder.



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

def initialize(charset, string, replace_char)
  _validate_input_parameters(charset, string, replace_char)

  @charset = charset
  @string = string.upcase.gsub(/[^#{charset}]/, replace_char)
  @base = charset.length
end

Instance Method Details

#encodeObject

Based on the @jgn/radix50.rb gist.github.com/jgn/707543



18
19
20
21
22
23
# File 'lib/dec_radix_50/encoder.rb', line 18

def encode
  charset_values = @charset.split(//).each_with_index.to_h
  @string.chars.each_slice(3).map do |x, y, z|
    (((charset_values[x] || 0) * @base + (charset_values[y] || 0)) * @base + (charset_values[z] || 0))
  end
end