From: Robert Klemme Date: 2008-09-10T18:40:07+09:00 Subject: Re: yield/block question 2008/9/10 Kenneth McDonald : > On Sep 9, 2008, at 7:17 PM, Serabe wrote: > >> What about this? >> >> class Array >> def each_leaf(&block) >> each do |x| >> case x >> when Array: x.each_leaf &block >> else block.call x >> end >> end >> end >> end 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 Kind regards robert -- use.inject do |as, often| as.you_can - without end