Sha256: 1018de851b9b47f38358f8d2e905ace95edcb6e67f1da544cea3ee982b700461

Contents?: true

Size: 900 Bytes

Versions: 7

Compression:

Stored size: 900 Bytes

Contents

class Array

  # Returns a new array created by traversing the array and its
  # sub-arrays, executing the given block on the elements.
  #
  #   h = ["A", "B", ["X", "Y"]]
  #
  #   g = h.traverse{ |e| e.downcase }
  #
  #   g  #=> ["a", "b", ["x", "y"]]
  #
  # This is the same as <code>recursive.map</code> and will
  # likely be deprecated in the future because of it.
  #
  # CREDIT: Trans
  def traverse(&block)
    if block_given?
      map do |e|
        if e.respond_to?(:to_ary)
          e.to_ary.traverse(&block)
        else
          block.call(e)
        end
      end
    else
      to_enum(:traverse)
    end
  end

  # Like #recursive_map, but will change the array in place.
  #
  #   h = ["A", "B", ["X", "Y"]]
  #
  #   h.traverse!{ |e| e.downcase }
  #
  #   h  #=> ["a", "b", ["x", "y"]]
  #
  # CREDIT: Trans
  def traverse!(&block)
    replace(traverse(&block))
  end

end

Version data entries

7 entries across 6 versions & 1 rubygems

Version Path
facets-2.9.3 lib/core/facets/array/traverse.rb
facets-2.9.2 src/core/facets/array/traverse.rb
facets-2.9.2 lib/core/facets/array/traverse.rb
facets-2.9.1 lib/core/facets/array/traverse.rb
facets-2.9.0 lib/core/facets/array/traverse.rb
facets-2.9.0.pre.2 lib/core/facets/array/traverse.rb
facets-2.9.0.pre.1 lib/core/facets/array/traverse.rb