Module: Encoder

Includes:
Constants
Included in:
RunLengthEncoding
Defined in:
lib/run_length_encoding/encoder.rb

Overview

Encode data with RLE.

Constant Summary

Constants included from Constants

Constants::CHUNK_KEY, Constants::COUNT_KEY

Instance Method Summary collapse

Instance Method Details

#encode(data, separator = "") ⇒ Array<Hash{ Symbol => Object }>

Check and then pass data to the actual encoder.

Parameters:

  • data (Array, String, Enumerator)

    Data to encode.

  • separator (String, Regexp) (defaults to: "")

    (“”) Separator for the string arguments.

Returns:

  • (Array<Hash{ Symbol => Object }>)

    Encoded data. Each element is a Hash with the following key-value pairs:

    [Constants::CHUNK_KEY => Object]
      A repeated object.
    
    [Constants::COUNT_KEY => Integer]
      How many times the object is repeated.
    

Raises:

  • (ArgumentError, TypeError)

    Raises if data isn’t an Array/String/Enumerator [TypeError] or an empty Array [ArgumentError].



31
32
33
34
35
36
37
# File 'lib/run_length_encoding/encoder.rb', line 31

def encode(data, separator = "")
  valid_data, ex_details = _check_enc_data(data, separator)

  raise(ex_details[:ex_type], ex_details[:ex_msg]) if ex_details

  _encode(valid_data)
end