Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Re: accessing the count/iteration # within an "each" ???
Tim Hunter
6/15/2007 9:41:00 PM
Greg Hauptmann wrote:
> Is there a way to access the count / iteration# within an "each"
> loop? i.e.
> how many times through the loop one is?
ruby$ ri Enumerable#each_with_index
--------------------------------------------- Enumerable#each_with_index
enum.each_with_index {|obj, i| block } -> enum
------------------------------------------------------------------------
Calls block with two arguments, the item and its index, for each
item in enum.
hash = Hash.new
%w(cat dog wombat).each_with_index {|item, index|
hash[item] = index
}
hash #=> {"cat"=>0, "wombat"=>2, "dog"=>1}
--
RMagick OS X Installer [
http://rubyforge.org/project...
]
RMagick Hints & Tips [
http://rubyforge.org/forum/forum.php?for...
]
RMagick Installation FAQ [
http://rmagick.rubyforge.org/instal...
]
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Re: accessing the count/iteration # within an "each" ???
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password