Method: Array#recurse

Defined in:
lib/core/facets/array/recurse.rb

#recurse(*types) {|a| ... } ⇒ Object

Apply a block to array, and recursively apply that block to each sub-array or types.

arr = ["a", ["b", "c", nil], nil]
arr.recurse{ |a| a.compact! }
#=> ["a", ["b", "c"]]

Yields:

  • (a)


10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/core/facets/array/recurse.rb', line 10

def recurse(*types, &block)
  types = [self.class] if types.empty?
  a = inject([]) do |array, value|
    case value
    when *types
      array << value.recurse(*types, &block)
    else
      array << value
    end
    array
  end
  yield a
end