Module: Kicker::ArrayExt

Defined in:
lib/kicker/core_ext.rb

Instance Method Summary collapse

Instance Method Details

#take_and_map(pattern = nil, flatten_and_compact = true) ⇒ Object

Deletes elements from self for which the block evaluates to true. A new array is returned with those values the block returned. So basically, a combination of reject! and map.

a = [1,2,3]
b = a.take_and_map { |x| x * 2 if x == 2 }
b # => [4]
a # => [1, 3]

If pattern is specified then files matching the pattern will be taken.

a = [ 'bar', 'foo/bar' ]
b = a.take_and_map('*/bar') { |x| x }
b # => ['foo/bar']
a # => ['bar']

If flatten_and_compact is true, the result array will be flattened and compacted. The default is true.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/kicker/core_ext.rb', line 21

def take_and_map(pattern = nil, flatten_and_compact = true)
  took = []
  reject! do |x|
    next if pattern and !File.fnmatch?(pattern, x)
    if result = yield(x)
      took << result
    end
  end
  if flatten_and_compact
    took.flatten!
    took.compact!
  end
  took
end