From: dblack@... Date: 2006-11-21T13:46:15+09:00 Subject: Re: array question Hi -- On Tue, 21 Nov 2006, 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. Here's one way, along with some tests: require 'test/unit' class ChangeArrayTest < Test::Unit::TestCase def change_array(a) [*1...a.size-1].reverse.each {|i| a.insert(i,a[i]) } a end def test_empty_array assert_equal([], change_array([])) end def test_one_element_array assert_equal([1], change_array([1])) end def test_two_element_array assert_equal([1,2], change_array([1,2])) end def test_three_element_array assert_equal([1,2,2,3], change_array([1,2,3])) end def test_ten_element_array assert_equal([1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10], change_array([1,2,3,4,5,6,7,8,9,10])) end end 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