Does each over an array bring a counter/index with it?

If I'm "each"ing over an array, is there a built-in way to get the
numeric index of the item that I'm on, or do I have to increment my own
counter variable? I saw each_with_index but that seems to be for Hash,
not Array.

···

--
Posted via http://www.ruby-forum.com/.

Duane Morin wrote:

If I'm "each"ing over an array, is there a built-in way to get the numeric index of the item that I'm on, or do I have to increment my own counter variable? I saw each_with_index but that seems to be for Hash, not Array.

irb(main):001:0> [5,10,15].each_with_index{|x,i|
irb(main):002:1* puts "#{i}: #{x}"
irb(main):003:1> }
0: 5
1: 10
2: 15
=> [5, 10, 15]
irb(main):004:0>

···

--
Alex

each_with_index is available for arrays as well.

It's actually defined in Enumerable, which is mixed into Array and Hash:

   a = %w(eeny meeny miney mo)
   a.each_with_index { |thing,number|
     puts "#{number}: #{thing}"
   }

produces:

  0: eeny
  1: meeny
  2: miney
  3: mo

···

On 15/06/06, Duane Morin <dmorin@gmail.com> wrote:

If I'm "each"ing over an array, is there a built-in way to get the
numeric index of the item that I'm on, or do I have to increment my own
counter variable? I saw each_with_index but that seems to be for Hash,
not Array.

--
Posted via http://www.ruby-forum.com/\.

--
Rasputin :: Jack of All Trades - Master of Nuns
http://number9.hellooperator.net/

Duane Morin wrote:

If I'm "each"ing over an array, is there a built-in way to get the
numeric index of the item that I'm on, or do I have to increment my own
counter variable? I saw each_with_index but that seems to be for Hash,
not Array.

It's for Enumerable, which is mixed-in to Array and Hash, so you will be
able to use it.

Cheers,
Dave

Do you have to do

'require enumerable'

each time you want to use it, or should it be accessible by default?

Thanks,
-Sidney

···

On 6/15/06, Dave Burt <dave@burt.id.au> wrote:

Duane Morin wrote:
> If I'm "each"ing over an array, is there a built-in way to get the
> numeric index of the item that I'm on, or do I have to increment my own
> counter variable? I saw each_with_index but that seems to be for Hash,
> not Array.

It's for Enumerable, which is mixed-in to Array and Hash, so you will be
able to use it.

Cheers,
Dave

It is there by default.

j`ey
http://www.eachmapinject.com

···

On 6/15/06, Sidney Burks <sid137@gmail.com> wrote:

Do you have to do

'require enumerable'

each time you want to use it, or should it be accessible by default?

Thanks,
-Sidney

On 6/15/06, Dave Burt <dave@burt.id.au> wrote:
>
> Duane Morin wrote:
> > If I'm "each"ing over an array, is there a built-in way to get the
> > numeric index of the item that I'm on, or do I have to increment my
own
> > counter variable? I saw each_with_index but that seems to be for
Hash,
> > not Array.
>
> It's for Enumerable, which is mixed-in to Array and Hash, so you will be
> able to use it.
>
> Cheers,
> Dave
>

No, it's a mixin (a bit like OOP inheritance in other languages).
See:

http://whytheluckystiff.net/ruby/pickaxe/html/tut_modules.html

···

On 15/06/06, Sidney Burks <sid137@gmail.com> wrote:

Do you have to do

'require enumerable'

each time you want to use it, or should it be accessible by default?

--
Rasputin :: Jack of All Trades - Master of Nuns
http://number9.hellooperator.net/

You should be able to just use it. Maybe it may not be part of
Enumerable in prior versions, which may require the use of "require
'enumerable'", but I'm unsure.

···

On 6/15/06, Sidney Burks <sid137@gmail.com> wrote:

Do you have to do

'require enumerable'

each time you want to use it, or should it be accessible by default?

Thanks,
-Sidney

On 6/15/06, Dave Burt <dave@burt.id.au> wrote:
>
> Duane Morin wrote:
> > If I'm "each"ing over an array, is there a built-in way to get the
> > numeric index of the item that I'm on, or do I have to increment my own
> > counter variable? I saw each_with_index but that seems to be for Hash,
> > not Array.
>
> It's for Enumerable, which is mixed-in to Array and Hash, so you will be
> able to use it.
>
> Cheers,
> Dave
>

--
Matt

No, it's a mixin (a bit like OOP inheritance in other languages).

You mean a bit like interfaces.

···

--
Matt

Not really. Interfaces don't specify an implementation.

···

On 15/06/06, Matthew Harris <shugotenshi@gmail.com> wrote:

> No, it's a mixin (a bit like OOP inheritance in other languages).

You mean a bit like interfaces.

--
Rasputin :: Jack of All Trades - Master of Nuns
http://number9.hellooperator.net/

Ruby's Comparable module vs. Java's Comparable interface.

Ruby: (http://ruby-doc.org/core/classes/Comparable.html\)
Java: (http://java.sun.com/j2se/1.3/docs/api/java/lang/Comparable.html\)

Notice the `compareTo()' method in Java's Comparable is equivalent to
the `<=>' method which Ruby's Comparable module requires to be
defined.

This is how interfaces and mix-ins work.

Ruby's mix-ins are closer to interfaces than they are inheritance.
Inheritence implies subclassing, most of the time.

···

On 6/15/06, Dick Davies <rasputnik@gmail.com> wrote:

On 15/06/06, Matthew Harris <shugotenshi@gmail.com> wrote:
> > No, it's a mixin (a bit like OOP inheritance in other languages).
>
> You mean a bit like interfaces.

Not really. Interfaces don't specify an implementation.

--
Matt

Interfaces provide typing.

Inheritance provides typing and implementation

Mixins provide implementation (and typing in Ruby anyway, but they don't _have_ to).

I would say Mixins are closer to inheritance than interfaces (esp. in a "duck-typed" language).

···

On Jun 15, 2006, at 7:57 AM, Matthew Harris wrote:

Ruby's mix-ins are closer to interfaces than they are inheritance.
Inheritence implies subclassing, most of the time.