From: "Kroeger, Simon (ext)" Date: 2005-10-19T00:29:53+09:00 Subject: Re: Unit testing an each function > -----Original Message----- > From: Peter Hickman [mailto:peter@semantico.com] > Sent: Tuesday, October 18, 2005 5:25 PM > To: ruby-talk ML > Subject: Re: Unit testing an each function > > James Edward Gray II wrote: > > > On Oct 18, 2005, at 10:04 AM, Peter Hickman wrote: > > > >> # What I want to work is > >> > >> data.each.each{|i| puts i} > > > > > > If you know there's going to be Enumerable objects in > there, perhaps > > you could redefine each() to handle the nesting, or provide > another > > iterator for this... > > > > James Edward Gray II > > It's no great hardship it is just that I was taken aback when > data.each.each{...} didn't work, it seemed like the right > thing to type > at that point. does this look more natural to you? class Array def flatten inject([]){|m, o| o.respond_to?(:flatten) ? m + o.flatten : m << o} end end class Range def flatten inject([]){|m, o| o.respond_to?(:flatten) ? m + o.flatten : m << o} end end data = Array.new data << (1..5) data << (10..20) data.flatten.each{|i| p i} cheers Simon