On Thu, 18 Jan 2001  00:13:23 +0900, Hugh Sasse Staff Elec Eng wrote:
> What is the idiomatic Ruby way to go through all the elements of an array,
> except the last one, gettting the index?
> 
> my_array.each_index {
>     |i|
>     unless i == my_array.last {
>         # ...
>     }
> }
> 
> seems less than neat.

exactly where Haskell list-lingo (head, tail, init, last) comes in
handy. On Array

,----
| def init
| 	self[0..-2]
| end
`----

and you can say

,----
| my_array.init.each_index{ |i|
| 	...
| }
`----

and of course you could directly write

,----
| my_array[0..-2].each_index{ |i|
| 	...
| }
`----

which seems less intuitive. (But is a direct answer to your Q)

See http://www.xs4all.nl/~hipster/lib/ruby/haskell for more Haskell
list-ops (it's incomplete though, if I only had the time ;). Notice
how e.g. foldl1, scanl1, split_at, take_while and drop_while build
nicely from earlier defined primitives as take, drop, tail and init.

	Michel