Robert Klemme wrote:
> On 21.11.2006 08:18, William James wrote:
> > Li Chen wrote:
> >> Hi all,
> >>
> >> I want to build a new array from an old one with every element being
> >> duplicated except the first and last element. And here are my codes. I
> >> wonder if this is a real Ruby way to do it.
> >
> >>> array=[1,2,3,4,5,6,7,8,9,10]
> > => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
> >>> array.zip(array).flatten[1..-2]
> > => [1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10]
>
> That's cute!  I have another one with #inject (of course):
>
> irb(main):007:0> arr=(1..10).to_a
> => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
> irb(main):008:0> copy=[]
> => []
> irb(main):009:0> arr.inject{|a,b| copy << a << b; b}
> => 10
> irb(main):010:0> copy
> => [1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10]
>
> Kind regards
>
> 	robert

This demonstrates an excellent understanding of inject and
is a good way to eliminate the somewhat ugly [1..-2].

Here's a prolix way of avoiding array indexing while using zip:

copy=arr.dup; copy.shift; copy.pop
arr.zip(copy).flatten.compact