Sha256: 6e44e6455315f2dab7c6c9daac2ebb8d95aabe7ae9ac898f5db7ba350e6b5a90

Contents?: true

Size: 584 Bytes

Versions: 3

Compression:

Stored size: 584 Bytes

Contents

class Array

  # Apply a block to array, and recursively apply that block
  # to each subarray.
  #
  #   arr = ["a", ["b", "c", nil], nil]
  #   arr.recursively{|a| a.compact! }
  #   => ["a", ["b", "c"]]
  #
  # TODO: Can this be generalized in Enumerbale?

  def recursively(&block)
    a = inject([]) do |array, value|
      if value.is_a?(Array)
        array << value.recursively(&block)
      else
        array << value
      end
      array
    end
    yield a
  end

  # In place form of #recursively.

  def recursively!(&block)
    replace(recursively(&block))
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
facets-2.8.2 lib/core/facets/array/recursively.rb
facets-2.8.1 lib/core/facets/array/recursively.rb
facets-2.8.0 lib/core/facets/array/recursively.rb