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.

Duane M. 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>

each_with_index is available for arrays as well.

Duane M. 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

It is there by default.

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

On 15/06/06, Sidney B. [email protected] wrote:

Do you have to do

‘require enumerable’

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

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

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

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 15/06/06, Matthew H. [email protected] 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.

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

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

You mean a bit like interfaces.

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 Jun 15, 2006, at 7:57 AM, Matthew H. wrote:

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

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).