counting .each

M

Mk 27

Is there some equivalent to file.lineno when using array.each? Eg, if I
am iterating thru an array of strings searching for a pattern, and I
want to save the current array index, do I have to do this:

count = 0
array.each { |e|
finds.push count if e =~ /pattern/
count += 1
}

or is there a "array.currentindex"? I guess I could use each_index...
 
R

Rick DeNatale

Is there some equivalent to file.lineno when using array.each? =A0Eg, if = I
am iterating thru an array of strings searching for a pattern, and I
want to save the current array index, do I have to do this:

count =3D 0
array.each { |e|
=A0 =A0finds.push count if e =3D~ /pattern/
=A0 =A0count +=3D 1
}

or is there a "array.currentindex"? =A0I guess I could use each_index...

array.each_with_index do | e, count|
finds.push count if e =3D~ /pattern/
end
--=20
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top