From: Saravanan Krishnan Date: 2011-03-25T16:26:52+09:00 Subject: Re: break from select Hi Venkat, Quick Solution, a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] b =[] a.collect{|x| b << x if x %2 == 0 and b.length<5} puts b.inspect Cheers, Sarav Venkat Surabhi wrote in post #989134: > Hi, > > I have an array like a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14] > I need to select elements which are divisible by 2 and select only > 5 elements. > > col = a.select do |e| > e %2 == 0 > end > > I get all the elements into col which are divisible by 2 and working > fine. > > what if I want to select only 5 elements > I am trying below code > i = 0 > col = a.select do |e| > i += 1 > break if i > 5 > e %2 == 0 > end > > Now col returns nil. > Also used return but getting error. > > Any solution for this. > > Thanks, > Venkat -- Posted via http://www.ruby-forum.com/.