Counting .each


#1

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…


#2

On Mon, Jun 1, 2009 at 7:06 PM, Mk 27 removed_email_address@domain.invalid
wrote:

or is there a “array.currentindex”? I guess I could use each_index…
array.each_with_index do | e, count|
finds.push count if e =~ /pattern/
end

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