Strange about Array#each_index

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>

Hi,

In message “Re: Strange about Array#each_index”
on Tue, 26 Dec 2006 03:51:19 +0900, Li Chen [email protected]
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.

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 :

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

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.

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

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