Sha256: c030cd2c4084ed93ca61687cdb2e1be107920354010aae140d2349d5e941f763

Contents?: true

Size: 747 Bytes

Versions: 26

Compression:

Stored size: 747 Bytes

Contents

#!/usr/bin/env ruby
# frozen_string_literal: true

# encoding=utf-8

class Array
  def pluck(key)
    map { |hash| hash[key] if hash.is_a?(Hash) }.compact
  end

  # 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.
  # @return [Array] The modified array after conditional deletions.
  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
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
markdown_exec-2.7.2 lib/array.rb
markdown_exec-2.7.1 lib/array.rb
markdown_exec-2.7.0 lib/array.rb
markdown_exec-2.6.0 lib/array.rb
markdown_exec-2.5.0 lib/array.rb
markdown_exec-2.4.0 lib/array.rb
markdown_exec-2.3.0 lib/array.rb
markdown_exec-2.2.0 lib/array.rb
markdown_exec-2.1.0 lib/array.rb
markdown_exec-2.0.8.4 lib/array.rb
markdown_exec-2.0.8.3 lib/array.rb
markdown_exec-2.0.8.2 lib/array.rb
markdown_exec-2.0.8.1 lib/array.rb
markdown_exec-2.0.8 lib/array.rb
markdown_exec-2.0.7 lib/array.rb
markdown_exec-2.0.6 lib/array.rb
markdown_exec-2.0.5 lib/array.rb
markdown_exec-2.0.4 lib/array.rb
markdown_exec-2.0.3.2 lib/array.rb
markdown_exec-2.0.3.1 lib/array.rb