From: David Alan Black Date: 2000-12-17T23:03:09+09:00 Subject: [ruby-talk:7505] Re: flatten_by(n) On Sun, 17 Dec 2000, Ben Tilly wrote: > I am away from my home box again, but something like this > should work: > > class Array > def flatten_once > res = [] > each do |e| > if e.is_a? Array > res += e > else > res.push e > end > end > res > end > end And, what's more.... def flatten_once res = [] for e in self # a little faster than each res.push(*e) end res end 10000 iterations: user system total real mine: 1.210000 0.040000 1.250000 ( 1.268839) yours: 3.040000 0.000000 3.040000 ( 3.042355) This is the part I love -- where the code almost evaporates, and actually gets faster and stronger. (Whose .sig was it that says something about gaining functionality by deleting code?) Also, just for fun (haven't tweaked this one yet): def flatten_once! res = flatten_once clear.concat res end David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav