V
Venkat Surabhi
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
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