From: Robert Klemme Date: 2006-05-16T03:01:17+09:00 Subject: Re: iterator/array question 2006/5/15, Daniel Harple : > On May 15, 2006, at 7:48 PM, Susan Sash wrote: > > > Is there a way to iterate through an array, getting the index and the > > object at the same time? > > $ri 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} Too bad you disclosed the solution already - I wanted to trade it in for those turtles... ;-) robert -- Have a look: http://www.flickr.com/photos/fussel-foto/