Class: Array

Inherits:
Object show all
Defined in:
lib/array.rb

Instance Method Summary collapse

Instance Method Details

#process_and_conditionally_delete!Array

Processes each element of the array, yielding the previous, current, and next elements to the given block. Deletes the current element if the block returns true.

Returns:

  • (Array)

    The modified array after conditional deletions.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/array.rb', line 10

def process_and_conditionally_delete!
  i = 0
  while i < length
    prev_item = self[i - 1] unless i.zero?
    current_item = self[i]
    next_item = self[i + 1]

    should_delete = yield prev_item, current_item, next_item
    if should_delete
      delete_at(i)
    else
      i += 1
    end
  end

  self
end