Y
Yannick Turgeon
Hello all,
I just started to learn Ruby and I've got a couple of questions concerning
arrays and particularly creating and returning "filtered" arrays.
--------------------------
1- Say I want to create an array of all pair numbers from 2 to 100, do I
have to write:
a = []
2.step(100, 2){|v| a.push(v)}
is there a way I could do in one single line:
a = ...
Or more generally, is there a way to collect and return, in an array, values
extracted at each loop inside a block without using a variable previously
created outside that block?
--------------------------
2- From this newly created array, if I want to extract only those numbers
mutiple of 5 (10,20, ...), are you coding something like:
b = a.collect{|v| v.modulo(5) == 0 ? v : nil}.compact
It seems like a miss a function here which should be called something like
"extract" or "find_all".
--------------------------
Thanks for your feedbacks.
Yannick
I just started to learn Ruby and I've got a couple of questions concerning
arrays and particularly creating and returning "filtered" arrays.
--------------------------
1- Say I want to create an array of all pair numbers from 2 to 100, do I
have to write:
a = []
2.step(100, 2){|v| a.push(v)}
is there a way I could do in one single line:
a = ...
Or more generally, is there a way to collect and return, in an array, values
extracted at each loop inside a block without using a variable previously
created outside that block?
--------------------------
2- From this newly created array, if I want to extract only those numbers
mutiple of 5 (10,20, ...), are you coding something like:
b = a.collect{|v| v.modulo(5) == 0 ? v : nil}.compact
It seems like a miss a function here which should be called something like
"extract" or "find_all".
--------------------------
Thanks for your feedbacks.
Yannick