From: Kaya Aykut on
Hello all, newbie here.

The title is quite self explanatory: I am in

dummy_array.each do |a|
#code here
end

and I want to know which element (the number n in nth element) of the
dummy_array.

Thanks in advance :-)
--
Posted via http://www.ruby-forum.com/.

From: James Harrison on
>
> The title is quite self explanatory: I am in
>
> dummy_array.each do |a|
> #code here
> end
>
> and I want to know which element (the number n in nth element) of the
> dummy_array.
>

You could just put a counting variable in there:


i = 0
> dummy_array.each do |a|
i = i+1
> #code here
> end

You can also use each_index on the array if you'd like.

http://ruby-doc.org/core/classes/Array.html#M002174

From: Nicholas Orr on
[Note: parts of this message were removed to make it a legal post.]

or use each_with_index
http://ruby-doc.org/core/classes/Enumerable.html#M003137

On Thu, Jul 29, 2010 at 8:41 AM, James Harrison <oscartheduck(a)gmail.com>wrote:

> >
> > The title is quite self explanatory: I am in
> >
> > dummy_array.each do |a|
> > #code here
> > end
> >
> > and I want to know which element (the number n in nth element) of the
> > dummy_array.
> >
>
> You could just put a counting variable in there:
>
>
> i = 0
> > dummy_array.each do |a|
> i = i+1
> > #code here
> > end
>
> You can also use each_index on the array if you'd like.
>
> http://ruby-doc.org/core/classes/Array.html#M002174
>
>

From: BruceL on
On Jul 28, 6:24 pm, Kaya Aykut <kaya.ay...(a)gmail.com> wrote:

dummy_array.each_index do |i|
a = array[i]
end