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.
Mk 2. (Guest)
on 2009-06-02 03: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...
Rick D. (Guest)
on 2009-06-02 03:58
(Received via mailing list)
On Mon, Jun 1, 2009 at 7:06 PM, Mk 27 <removed_email_address@domain.invalid>
> 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.