From: Joey Date: 2006-12-30T01:24:21+09:00 Subject: Re: Another Q: find behavior ------=_Part_101366_27299587.1167409459456 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Because of the "return value if yield(value)", which returns from the method causing the loop to end. I'm not sure what your really trying to achieve, there already is a Array#find j`ey On 12/29/06, Krekna Mektek wrote: > > Hi, > > Still learning from the Pickaxe book, and I've got a question about this > code: > > class Array > def find > for i in 0..size > value = self[i] > puts self[i] > return value if yield(value) > end > return nil > end > end > > [1,3,5,7,9].find {|v| v*v > 30 } > > irb(main):369:0> [1,3,5,7,9].find {|v| v*v > 30 } > => 7 > > > how come it prints only 7, and not also 9? > The loop is not stopping as far as I can see, and 9 times 9 is also > bigger then 30. > > Krekna > > ------=_Part_101366_27299587.1167409459456--