R
Ronald Fischer
For an array a, I would like to know all the indices i where a
fulfils
some condition. My solution (which works) looks like this:
a=3D%w(Deutschlandsberg Stainz Preding Eibiswald)
(0...a.size).select {|i| a=3D~/g$/} # =3D=3D> [0,2]
What I don't like with this solution is that inside the code block
supplied to select, I also have to access the array variable a which
is declared outside. I would prefer having my code block "self=20
contained", using only the variables passed as parameter.
Is there in Ruby a function similar select, where I also get the array
element passed through, kind of:
a.select_index {|index,value| value?~/g$/}
or do I have to write my own here? I don't want to reinvent the
wheel....
Ronald
--=20
Ronald Fischer <[email protected]>
Phone: +49-89-452133-162
fulfils
some condition. My solution (which works) looks like this:
a=3D%w(Deutschlandsberg Stainz Preding Eibiswald)
(0...a.size).select {|i| a=3D~/g$/} # =3D=3D> [0,2]
What I don't like with this solution is that inside the code block
supplied to select, I also have to access the array variable a which
is declared outside. I would prefer having my code block "self=20
contained", using only the variables passed as parameter.
Is there in Ruby a function similar select, where I also get the array
element passed through, kind of:
a.select_index {|index,value| value?~/g$/}
or do I have to write my own here? I don't want to reinvent the
wheel....
Ronald
--=20
Ronald Fischer <[email protected]>
Phone: +49-89-452133-162