K
Kenneth McDonald
This is something I know how to do in Python, but it's got me
flummoxed in Ruby--I still haven't fully gotten my head around the
Ruby way. ("yield" means something very different in Python and in Ruby.
Let's say I have a data structure consisting of nested Arrays and
numbers, for example [[1,2], 3, [[4]]]. I want to write an extension
to Array, "each_leaf", which takes a block that will be applied to
each number sequentially in that nested array. For example, if arr is
the given array, then
result = 0
arr.each_leaf {|n| result += n}
would end with result being equal to 1+2+3+4
Could anyone help out? Much appreciated.
Thanks,
Ken
flummoxed in Ruby--I still haven't fully gotten my head around the
Ruby way. ("yield" means something very different in Python and in Ruby.
Let's say I have a data structure consisting of nested Arrays and
numbers, for example [[1,2], 3, [[4]]]. I want to write an extension
to Array, "each_leaf", which takes a block that will be applied to
each number sequentially in that nested array. For example, if arr is
the given array, then
result = 0
arr.each_leaf {|n| result += n}
would end with result being equal to 1+2+3+4
Could anyone help out? Much appreciated.
Thanks,
Ken