Iterate over an array with the index?

Hi all

Is there a method to iterate over an array with the index? Something
like

my_array.each_with_index do |obj, index|

end

Just couldn’t find such a method… maybe I worked too much today… :wink:

Thanks
Josh

On Sep 27, 1:36 am, Joshua M. [email protected]
wrote:

Thanks
Josh

Posted viahttp://www.ruby-forum.com/.

Won’t each_with_index suffice?

Joshua M. wrote:

Hi all

Is there a method to iterate over an array with the index? Something
like

my_array.each_with_index do |obj, index|

end

Just couldn’t find such a method… maybe I worked too much today… :wink:

Thanks
Josh

Hi Josh,

I agree that you worked too much today. Ruby’s Enumerable class has that
exact method.

[1, 2].respond_to?(:each_with_index)
=> true

Peace.

What was I thinking…? :wink:

Thanks anyway :slight_smile: