Strange about Array#each_index

L

Li Chen

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

côme

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 :
 
L

Li Chen

côme said:
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
 
R

Robert Klemme

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
 
P

Phrogz

Robert said:
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.
 
L

Li Chen

Robert said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,221
Messages
2,571,134
Members
47,748
Latest member
LyleMondra

Latest Threads

Top