Class: RunLengthEncoder::Instance
- Inherits:
-
Object
- Object
- RunLengthEncoder::Instance
- Defined in:
- lib/run_length_encoder.rb
Instance Method Summary collapse
- #decode(encoded) ⇒ Object (also: #load)
- #encode(string_or_array) ⇒ Object (also: #dump)
-
#initialize(count_separator: ":", term_separator: ";", converter: default_converter) ⇒ Instance
constructor
A new instance of Instance.
Constructor Details
#initialize(count_separator: ":", term_separator: ";", converter: default_converter) ⇒ Instance
Returns a new instance of Instance.
24 25 26 27 28 |
# File 'lib/run_length_encoder.rb', line 24 def initialize(count_separator: ":", term_separator: ";", converter: default_converter) @count_separator = count_separator @term_separator = term_separator @converter = converter end |
Instance Method Details
#decode(encoded) ⇒ Object Also known as: load
42 43 44 45 46 47 |
# File 'lib/run_length_encoder.rb', line 42 def decode(encoded) terms = encoded.split(@term_separator) splits = terms.map { |term| term.split(@count_separator) } splits2 = splits.map { |(count, char)| [count.to_i, char] } @converter[splits2] end |
#encode(string_or_array) ⇒ Object Also known as: dump
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/run_length_encoder.rb', line 30 def encode(string_or_array) case string_or_array when String _encode(string_or_array.each_char) when Array _encode(string_or_array.each) else raise "Unsupported type: #{string_or_array.class}" end end |