From: dblack@... Date: 2003-05-30T10:41:21+09:00 Subject: Re: Array.extend versus instance.extend Hi -- On Fri, 30 May 2003, Simon Strandgaard wrote: > On Fri, 30 May 2003 01:15:32 +0900, Guillaume Marcais wrote: > > > module ArrayMisc2 > > def shift_until(klass) > > p = -1 > > detect { |x| p += 1; klass === x } > > slice!(0, p) > > end > > def pop_until(klass) > > p = -1 > > reverse.detect { |x| p += 1; klass === x } > > slice!(-p, p) > > end > > end > > > Sorry your code does not pass all the tests.. it has somekind of > different behavier. I have never tried using 'Enumable#detect' before > so I cannot figure out exactly what seems to be the problem. > Thanks for trying :-) #detect returns the first element for which the block is true, passing each element to the block in turn: [1,2,3,4].detect {|e| e > 2} # 3 Here's a modification of Guillaume's code that passes your tests: module ArrayMisc2A def shift_until(klass) p = 0 detect { |x| p += 1 and klass === x } slice!(0, p) end def pop_until(klass) p = 0 reverse.detect { |x| p += 1 and klass === x } slice!(-p, p) end end David -- David Alan Black home: dblack@superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav