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