Forum: Ruby counting .each

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
68814ed988eee4f21b2cd07353e5e445?d=identicon&s=25 Mk 27 (mk27)
on 2009-06-02 01:06
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...
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2009-06-02 01:58
(Received via mailing list)
On Mon, Jun 1, 2009 at 7:06 PM, Mk 27 <>
> 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/
Rick DeNatale

This topic is locked and can not be replied to.