Class: RunLengthEncoder::Instance

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

Instance Method Summary collapse

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