Forum: Ruby counting .each

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community 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 <halfcountplus@intergate.com>
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
This topic is locked and can not be replied to.