# Strange about Array#each\_index

**URL:** <https://www.ruby-forum.com/t/strange-about-array-each-index/80392>\
**Category:** Ruby\
**Created:** [December 25, 2006, 7:51pm UTC](https://www.ruby-forum.com/t/strange-about-array-each-index/80392 "2006-12-25T19:51:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 25, 2006, 7:51pm UTC](https://www.ruby-forum.com/t/strange-about-array-each-index/80392/1 "2006-12-25T19:51:17Z")

</div>

Hi all,

Happy holidays!

I query Ruby about Array#each\_index and here are what Ruby returns:

## --------------------------------------------------- Array#each\_index array.each\_index {|index| block } -\> array

Same as +Array#each+, but passes the index of the element instead  
of the element itself.

```
a = ["a", "b", "c"]
a.each_index {|x| print x, " -- " }

```

produces:

```
0 -- 1 -- 2 --

```

According to the above Array#each\_index will return only the index for  
each element: 0,1,2.

But when I paste the codes and run on my XP I find Ruby returns both  
indexes and values. Do I miss something?

Thanks,

Li

### 

C:\>irb  
irb(main):001:0\> a = [“a”, “b”, “c”]  
=\> [“a”, “b”, “c”]  
irb(main):002:0\> a.each\_index {|x| print x, " – " }  
0 – 1 – 2 – =\> [“a”, “b”, “c”]  
irb(main):003:0\>

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 25, 2006, 8:10pm UTC](https://www.ruby-forum.com/t/strange-about-array-each-index/80392/2 "2006-12-25T20:10:06Z")

</div>

Hi,

In message “Re: Strange about Array#each\_index”  
on Tue, 26 Dec 2006 03:51:19 +0900, Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:

|I query Ruby about Array#each\_index and here are what Ruby returns:  
|  
|--------------------------------------------------- Array#each\_index  
| array.each\_index {|index| block } → array  
|--------------------------------------------------------------------  
| Same as +Array#each+, but passes the index of the element instead  
| of the element itself.

|C:\>irb  
|irb(main):001:0\> a = [“a”, “b”, “c”]  
|=\> [“a”, “b”, “c”]  
|irb(main):002:0\> a.each\_index {|x| print x, " – " }  
|0 – 1 – 2 – =\> [“a”, “b”, “c”]  
|irb(main):003:0\>

each\_index returns the receiver, and irb prints the return value.

```
						matz.

```

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 25, 2006, 8:12pm UTC](https://www.ruby-forum.com/t/strange-about-array-each-index/80392/3 "2006-12-25T20:12:07Z")

</div>

Hi,

Try it in a program file : irb use to print the “inspect” method of  
every object used.

ex :  
irb(main):006:0\> 1  
=\> 1

or :

irb(main):004:0\> b= a.each\_index {|x| print x, " – " }  
0 – 1 – 2 – =\> [“a”, “b”, “c”]  
irb(main):005:0\> b  
=\> [“a”, “b”, “c”]

Li Chen a Ã©crit :

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 25, 2006, 8:22pm UTC](https://www.ruby-forum.com/t/strange-about-array-each-index/80392/4 "2006-12-25T20:22:23Z")

</div>

cÃ´me wrote:

> Try it in a program file : irb use to print the “inspect” method of  
> every object used.

Thank you all.

It looks like there are some differences between running a script from  
irb and the command line. I should try them both before I post questions  
in the future.

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 25, 2006, 9:36pm UTC](https://www.ruby-forum.com/t/strange-about-array-each-index/80392/5 "2006-12-25T21:36:23Z")

</div>

Robert K. wrote:

> each\_index always _returns_ the receiver (i.e. the Array or whatever you  
> invoke that method on) and each\_index _passes_ every index to the block.

A few more examples to help clarify:

# Do nothing in the block and see what is returned

p %w|a b c|.each\_index{ }

# =\> [“a”, “b”, “c”]

# How about the #times method?

p 3.times{ }  
#=\> 3

# Apparently it returns the last index passed to the block

# Let’s write our own

def foo; yield; end  
p foo{ ‘a’ }  
#=\> ‘a’

# Our method returns whatever the block returns

# Now, let’s return something else

def foo  
yield 42  
‘bar’  
end  
p foo{ |x| p x }  
#=\> 42  
#=\> “bar”

That last example is like each\_index; it doesn’t matter what you do in  
the block, the method ends up returning a different value from what  
your block returns.

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 25, 2006, 9:45pm UTC](https://www.ruby-forum.com/t/strange-about-array-each-index/80392/6 "2006-12-25T21:45:03Z")

</div>

Robert K. wrote:

> So the crucial bit to  
> distinguish is _passing_ values to a block and _returning_ values from a  
> method.

If I underdstand correctly the best means to confirm what you say is to  
run a script from the command promt using a progam file. Am I right?

Thanks,

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 25, 2006, 8:52pm UTC](https://www.ruby-forum.com/t/strange-about-array-each-index/80392/7 "2006-12-25T20:52:11Z")

</div>

On 25.12.2006 20:22, Li Chen wrote:

> côme wrote:
> 
> > Try it in a program file : irb use to print the “inspect” method of  
> > every object used.
> 
> It looks like there are some differences between running a script from  
> irb and the command line. I should try them both before I post questions  
> in the future.

I am not sure whether you understood the point properly - please ignore  
if I’m just adding line noise. Yes, there are differences in the  
treatment of local variables. IRB also prints out the result of  
invoking #inspect on each expression that you pass on to evaluation.  
Other than that each\_index behaves identically in IRB and in a script.  
In your first posting you say

> According to the above Array#each\_index will return only the index for  
> each element: 0,1,2.

and

> But when I paste the codes and run on my XP I find Ruby returns both  
> indexes and values. Do I miss something?

each\_index always _returns_ the receiver (i.e. the Array or whatever you  
invoke that method on) and each\_index _passes_ every index to the block.  
The fact that you see both is just a consequence of the fact that both  
routes eventually print something to the screen. So the crucial bit to  
distinguish is _passing_ values to a block and _returning_ values from a  
method.

Kind regards

```
robert
```
