Method: Array#traverse

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

#traverse(&block) ⇒ Object

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 recursive.map and will likely be deprecated in the future because of it.

Returns new array. [Array]

CREDIT: Trans



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/core/facets/array/traverse.rb', line 19

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