Sha256: dfc2a22a26a240c7e1470ba41abb484253e316dae96cc49f594f6296d62c33ab

Contents?: true

Size: 988 Bytes

Versions: 3

Compression:

Stored size: 988 Bytes

Contents

class Array

  # Construct a new array created by traversing the array and its
  # sub-arrays, executing the given block on the elements.
  #
  # Examples
  #
  #   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.
  #
  # Returns new array. [Array]
  #
  # 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.
  #
  # Examples:
  #
  #   h = ["A", "B", ["X", "Y"]]
  #   h.traverse!{ |e| e.downcase }
  #   h  #=> ["a", "b", ["x", "y"]]
  #
  # Returns self. [Array]
  #
  # CREDIT: Trans

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

end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/core/facets/array/traverse.rb
facets-3.1.0 lib/core/facets/array/traverse.rb
facets-3.0.0 lib/core/facets/array/traverse.rb