From: Mayank Kohaley Date: 2011-03-25T16:23:46+09:00 Subject: Re: break from select --001485f89424646f47049f4979fc Content-Type: text/plain; charset=ISO-8859-1 a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] a = a.select { |x| x%2 == 0 } a = a.slice(0,5) puts "#{a.inspect}" Is this what you are expecting? On Fri, Mar 25, 2011 at 12:35 PM, Venkat Surabhi wrote: > 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/. > > -- Mayank Kohaley --001485f89424646f47049f4979fc--