Sha256: 40c1f3c00e793ba1a3ca8f027086fd7236e5255a0700e202a58b1a5d7ad30b7a

Contents?: true

Size: 582 Bytes

Versions: 10

Compression:

Stored size: 582 Bytes

Contents

class Array

  # Apply a block to hash, and recursively apply that block
  # to each subhash.
  #
  #   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

10 entries across 10 versions & 2 rubygems

Version Path
facets-2.7.0 lib/core/facets/array/recursively.rb
facets-2.6.0 lib/core/facets/array/recursively.rb
facets-2.4.3 lib/core/facets/array/recursively.rb
facets-2.4.4 lib/core/facets/array/recursively.rb
facets-2.4.2 lib/core/facets/array/recursively.rb
facets-2.5.0 lib/core/facets/array/recursively.rb
facets-2.4.5 lib/core/facets/array/recursively.rb
facets-2.5.1 lib/core/facets/array/recursively.rb
facets-2.5.2 lib/core/facets/array/recursively.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/core/facets/array/recursively.rb