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
On Tue, Sep 9, 2008 at 5:04 PM, Kenneth McDonald <kenneth.m.mcdonald@sbcglobal.net> wrote:
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
Very elegant, with the drawback that it requires flattening a potentially large array structure.
But that was carelessness in my lack of specifications. A nice solution, thank you.
Ken
···
On Sep 9, 2008, at 7:11 PM, Martin DeMello wrote:
On Tue, Sep 9, 2008 at 5:04 PM, Kenneth McDonald > <kenneth.m.mcdonald@sbcglobal.net> wrote:
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
This can be generalized to Enumerable. Still a handicap is that this
does not gracefully deal with structures which are not cycle free.
Just some toying around:
module Enumerable
def each_leaf(cl = Object, &b)
each do |el|
case el
when String
yield el if cl === el
when Enumerable
el.each_leaf(cl, &b)
when cl
yield el
else
# ignore
end
end
end
end