Class: Array

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

Instance Method Summary collapse

Instance Method Details

#pluck(key) ⇒ Object



7
8
9
# File 'lib/array.rb', line 7

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

#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.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/array.rb', line 14

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