[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Strange about Array#each_index

Li Chen

12/25/2006 6:51:00 PM

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>

--
Posted via http://www.ruby-....

5 Answers

user@domain.invalid

12/25/2006 7:05:00 PM

0

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

Li Chen

12/25/2006 7:22:00 PM

0

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

--
Posted via http://www.ruby-....

Robert Klemme

12/25/2006 7:48:00 PM

0

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

Gavin Kistner

12/25/2006 8:32:00 PM

0

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

Li Chen

12/25/2006 8:45:00 PM

0

Robert Klemme 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

--
Posted via http://www.ruby-....