From: dblack@... Date: 2006-11-21T13:46:52+09:00 Subject: Re: array question Hi -- On Tue, 21 Nov 2006, Wilson Bilkovich wrote: > On 11/20/06, 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. >> > > A couple of ways: (there are probably dozens more) > # given: > array = [1,2,3,4,5,6] > > # 1. > new_array = array[1..-2] > > # 2. > new_array = array.dup > new_array.shift # shifts off the first element > new_array.pop # pops off the last element I think Li wanted to duplicate elements in the sense of: [1,2,3,4,5] => [1,2,2,3,3,4,4,5] David -- David A. Black | dblack@rubypal.com Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org