From: Robert Klemme Date: 2006-12-26T04:50:04+09:00 Subject: Re: Strange about Array#each_index 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