Class: ProconBypassMan::CompressArray

Inherits:
Object
  • Object
show all
Defined in:
lib/procon_bypass_man/support/compress_array.rb

Defined Under Namespace

Classes: CompressibleValue

Instance Method Summary collapse

Constructor Details

#initialize(array) ⇒ CompressArray

Returns a new instance of CompressArray.



30
31
32
# File 'lib/procon_bypass_man/support/compress_array.rb', line 30

def initialize(array)
  @array = array
end

Instance Method Details

#compressArray<String>

Returns:

  • (Array<String>)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/procon_bypass_man/support/compress_array.rb', line 35

def compress
  previous_value = nil
  @array.reduce([]) do |acc, item|
    if previous_value.nil?
      acc << item
      previous_value = item
      next acc
    end

    if CompressibleValue.new(previous_value, item).compress?
      registered_value = acc.pop
      acc << CompressibleValue.new(registered_value, item).to_s_with_mark
    else
      acc << item
    end

    previous_value = item
    next acc
  end
end