Method: Array#traverse

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

#traverse(&block) ⇒ Object

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

CREDIT: Trans



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/core/facets/array/traverse.rb', line 16

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