# Array question

**URL:** <https://rubytalk.org/t/array-question/33112>\
**Category:** ruby-talk\
**Created:** [21 November 2006 04:29 UTC](https://rubytalk.org/t/array-question/33112 "2006-11-21T04:29:32Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guest2](https://avatars.discourse-cdn.com/v4/letter/g/439d5e/32.png) [@Guest2](https://rubytalk.org/u/Guest2)\
**Post date:** [21 November 2006 04:29 UTC](https://rubytalk.org/t/array-question/33112/1 "2006-11-21T04:29:32Z")

</div>

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.

Thank you for your input.

Li

> **···**
>
> #########  
> C:\\>irb  
> irb(main):001:0\> array=[1,2,3,4,5,6,7,8,9,10]  
> =\> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  
> irb(main):002:0\> array\_new=Array.new  
> =\> []  
> irb(main):003:0\> array.each do |e|  
> irb(main):004:1\* array\_new\<\<"#{e}"  
> irb(main):005:1\> array\_new\<\<"#{e}"  
> irb(main):006:1\> end  
> =\> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  
> irb(main):007:0\>  
> irb(main):008:0\* array\_new.delete\_at(0)  
> =\> "1"  
> irb(main):009:0\> array\_new.delete\_at(array\_new.size-1)  
> =\> "10"  
> irb(main):010:0\> puts array\_new  
> 1  
> 2  
> 2  
> 3  
> 3  
> 4  
> 4  
> 5  
> 5  
> 6  
> 6  
> 7  
> 7  
> 8  
> 8  
> 9  
> 9  
> 10  
> =\> nil
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Wilson\_Bilkovich](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/wilson_bilkovich/32/8247_2.png) [@Wilson\_Bilkovich](https://rubytalk.org/u/Wilson_Bilkovich)\
**Post date:** [21 November 2006 04:38 UTC](https://rubytalk.org/t/array-question/33112/2 "2006-11-21T04:38:26Z")

</div>

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

#1 uses Array#, which is a synonym for Array#slice.  
1..-2 is a Range parameter. In this case, it says you want a slice of  
the array containing everything but the first and last element.

> **···**
>
> On 11/20/06, Li Chen \<chen\_li3@yahoo.com\> 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.

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [21 November 2006 04:46 UTC](https://rubytalk.org/t/array-question/33112/3 "2006-11-21T04:46:15Z")

</div>

Hi --

> 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

&nbsp;&nbsp;&nbsp;def change\_array(a)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[\*1...a.size-1].reverse.each {|i| a.insert(i,a[i]) }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def test\_empty\_array  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal(, change\_array())  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def test\_one\_element\_array  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal([1], change\_array([1]))  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def test\_two\_element\_array  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal([1,2], change\_array([1,2]))  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def test\_three\_element\_array  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal([1,2,2,3], change\_array([1,2,3]))  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def test\_ten\_element\_array  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal([1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;change\_array([1,2,3,4,5,6,7,8,9,10]))  
&nbsp;&nbsp;&nbsp;end  
end

David

> **···**
>
> On Tue, 21 Nov 2006, Li Chen wrote:
> 
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
> [2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![Florian\_Frank2](https://avatars.discourse-cdn.com/v4/letter/f/a587f6/32.png) [@Florian\_Frank2](https://rubytalk.org/u/Florian_Frank2)\
**Post date:** [21 November 2006 10:31 UTC](https://rubytalk.org/t/array-question/33112/4 "2006-11-21T10:31:42Z")

</div>

Li Chen schrieb:

> 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.  
> &nbsp;&nbsp;

c = a.size \> 2 ? a[0, 1] + a[1..-2].inject() { |b,x| b \<\< x \<\< (x.dup rescue x) } + a[-1, 1] : a

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [22 November 2006 17:45 UTC](https://rubytalk.org/t/array-question/33112/5 "2006-11-22T17:45:34Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![Daniel\_N1](https://avatars.discourse-cdn.com/v4/letter/d/df788c/32.png) [@Daniel\_N1](https://rubytalk.org/u/Daniel_N1)\
**Post date:** [21 November 2006 04:45 UTC](https://rubytalk.org/t/array-question/33112/6 "2006-11-21T04:45:29Z")

</div>

This would not duplicate the inner elements though.

To build on this one.  
b = a.inject( ) { |a,e| a \<\< [e,e]}.flatten[1..-2]

This looks pretty jarring to my eyes though ☹

> **···**
>
> On 11/21/06, Wilson Bilkovich \<wilsonb@gmail.com\> wrote:
> 
> > \>
> > 
> > A couple of ways: (there are probably dozens more)  
> > # given:  
> > array = [1,2,3,4,5,6]
> > 
> > # 1.  
> > new\_array = array[1..-2]

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [21 November 2006 04:46 UTC](https://rubytalk.org/t/array-question/33112/7 "2006-11-21T04:46:52Z")

</div>

Hi --

> **···**
>
> On Tue, 21 Nov 2006, Wilson Bilkovich wrote:
> 
> > On 11/20/06, Li Chen \<chen\_li3@yahoo.com\> 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:
> 
> &nbsp;&nbsp;&nbsp;[1,2,3,4,5] =\> [1,2,2,3,3,4,4,5]
> 
> David
> 
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
> [2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![Guest2](https://avatars.discourse-cdn.com/v4/letter/g/439d5e/32.png) [@Guest2](https://rubytalk.org/u/Guest2)\
**Post date:** [21 November 2006 05:13 UTC](https://rubytalk.org/t/array-question/33112/8 "2006-11-21T05:13:14Z")

</div>

> 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
> 
> #1 uses Array#, which is a synonym for Array#slice.  
> 1..-2 is a Range parameter. In this case, it says you want a slice of  
> the array containing everything but the first and last element.

Hi,

I run array=[1,2,3,4,5]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_array = array.dup  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_array.shift  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_array.pop  
I get [2,3,4] but I want this result [1,2,2,3,3,4,4,5]

Li

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [22 November 2006 17:45 UTC](https://rubytalk.org/t/array-question/33112/9 "2006-11-22T17:45:40Z")

</div>

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

&nbsp;&nbsp;robert

> **···**
>
> 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]

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [22 November 2006 18:11 UTC](https://rubytalk.org/t/array-question/33112/10 "2006-11-22T18:11:04Z")

</div>

Hi --

> **···**
>
> On Thu, 23 Nov 2006, 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]
> 
> I \*still\* don't feel comfortable with that one 🙂 The flatten  
> thing is just too fragile.
> 
> David
> 
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;David A. Black | dblack@wobblini.net  
> Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]  
> DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
> [1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
> [2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![Daniel\_N1](https://avatars.discourse-cdn.com/v4/letter/d/df788c/32.png) [@Daniel\_N1](https://rubytalk.org/u/Daniel_N1)\
**Post date:** [21 November 2006 04:46 UTC](https://rubytalk.org/t/array-question/33112/11 "2006-11-21T04:46:29Z")

</div>

b = a.inject( ) { |a,e| a \<\< [e,e]}.flatten[1..-2] }

> **···**
>
> On 11/21/06, Daniel N \<has.sox@gmail.com\> wrote:
> 
> > On 11/21/06, Wilson Bilkovich \<wilsonb@gmail.com\> wrote:  
> > \>  
> > \> \>  
> > \>  
> > \> A couple of ways: (there are probably dozens more)  
> > \> # given:  
> > \> array = [1,2,3,4,5,6]  
> > \>  
> > \> # 1.  
> > \> new\_array = array[1..-2]
> > 
> > This would not duplicate the inner elements though.
> > 
> > To build on this one.  
> > b = a.inject( ) { |a,e| a \<\< [e,e]}.flatten[1..-2]
> > 
> > This looks pretty jarring to my eyes though ☹
> > 
> > Sorry missed a brace on the block. Should be

---

<div class="post-metadata">

**Author:** ![Wilson\_Bilkovich](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/wilson_bilkovich/32/8247_2.png) [@Wilson\_Bilkovich](https://rubytalk.org/u/Wilson_Bilkovich)\
**Post date:** [21 November 2006 05:18 UTC](https://rubytalk.org/t/array-question/33112/12 "2006-11-21T05:18:27Z")

</div>

Yeah. Sorry. I didn't read your question clearly enough. I apologize  
for confusing the issue. Check out the other replies from people with  
better reading comprehension late at night.  
Heh.

> **···**
>
> On 11/21/06, Li Chen \<chen\_li3@yahoo.com\> wrote:
> 
> > \> 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  
> > \>  
> > \> #1 uses Array#, which is a synonym for Array#slice.  
> > \> 1..-2 is a Range parameter. In this case, it says you want a slice of  
> > \> the array containing everything but the first and last element.
> > 
> > Hi,
> > 
> > I run array=[1,2,3,4,5]  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_array = array.dup  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_array.shift  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_array.pop  
> > I get [2,3,4] but I want this result [1,2,2,3,3,4,4,5]
> > 
> > Li

---

<div class="post-metadata">

**Author:** ![J\_B\_Rainsberger](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@J\_B\_Rainsberger](https://rubytalk.org/u/J_B_Rainsberger)\
**Post date:** [21 November 2006 06:03 UTC](https://rubytalk.org/t/array-question/33112/13 "2006-11-21T06:03:42Z")

</div>

Daniel N wrote:

> > \>
> > 
> > A couple of ways: (there are probably dozens more)  
> > # given:  
> > array = [1,2,3,4,5,6]
> > 
> > # 1.  
> > new\_array = array[1..-2]
> 
> This would not duplicate the inner elements though.
> 
> To build on this one.  
> b = a.inject( ) { |a,e| a \<\< [e,e]}.flatten[1..-2]
> 
> This looks pretty jarring to my eyes though ☹

I would use map over inject into :

b = a.map { |each| [each,each] }.flatten[1..-2]

Either that, or recognize that it's equivalent duplicating all except first and last.

b = [a[0], a[1..-2].map { |each| [each, each] }.flatten, a[-1]]

In either case, we can avoid the flatten entirely, at the expense of returning to inject:

b = a.inject() {|sum, each| sum.concat([each, each])[1..-2]  
or  
b = [a[0],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[1..-2].inject() {|sum, each| sum.concat([each, each])},  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[-1]]

Of course, if we know the items in the array are in ascending order, we can /really/ cheat:

b = (a\*2).sort[1..-2]

That's as many as I could think of.

> **···**
>
> > On 11/21/06, Wilson Bilkovich \<wilsonb@gmail.com\> wrote:
> 
> --  
> J. B. (Joe) Rainsberger :: [http://www.jbrains.ca](http://www.jbrains.ca)  
> Your guide to software craftsmanship  
> JUnit Recipes: Practical Methods for Programmer Testing  
> 2005 Gordon Pask Award for contribution Agile Software Practice

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [21 November 2006 10:58 UTC](https://rubytalk.org/t/array-question/33112/14 "2006-11-21T10:58:01Z")

</div>

On\_Behalf\_Of\_Li\_Chen:  
# I run array=[1,2,3,4,5]  
# new\_array = array.dup  
# new\_array.shift  
# new\_array.pop  
# I get [2,3,4] but I want this result [1,2,2,3,3,4,4,5]

but you can build from there. you just have to assemble it back, something like,

irb(main):031:0\> array=[1,2,3,4,5]  
=\> [1, 2, 3, 4, 5]  
irb(main):032:0\> new\_array = array.dup  
=\> [1, 2, 3, 4, 5]  
irb(main):035:0\> first = new\_array.shift  
=\> 1  
irb(main):036:0\> last = new\_array.pop  
=\> 5  
irb(main):037:0\> new\_array  
=\> [2, 3, 4]  
irb(main):038:0\> new\_array.map{|e| [e,e]}.flatten  
=\> [2, 2, 3, 3, 4, 4]  
irb(main):039:0\> new\_array.map{|e| [e,e]}.flatten.unshift(first)  
=\> [1, 2, 2, 3, 3, 4, 4]  
irb(main):040:0\> new\_array.map{|e| [e,e]}. flatten. unshift(first). push(last)  
=\> [1, 2, 2, 3, 3, 4, 4, 5]  
irb(main):041:0\>

kind regards -botp

# Li

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [22 November 2006 18:05 UTC](https://rubytalk.org/t/array-question/33112/15 "2006-11-22T18:05:32Z")

</div>

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
> > 
> > &nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [21 November 2006 04:50 UTC](https://rubytalk.org/t/array-question/33112/16 "2006-11-21T04:50:17Z")

</div>

Hi --

> **···**
>
> On Tue, 21 Nov 2006, Daniel N wrote:
> 
> > On 11/21/06, Daniel N \<has.sox@gmail.com\> wrote:
> > 
> > > Sorry missed a brace on the block. Should be
> > 
> > b = a.inject( ) { |a,e| a \<\< [e,e]}.flatten[1..-2] }
> 
> I so much think it's time for flatten to take an argument, specifying  
> number of levels to flatten. My flattenx extension does this, and it  
> seems like a natural fit for flatten itself. Otherwise almost all  
> flatten-based techniques run the risk of over-flattening.
> 
> David
> 
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
> [2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![Guest2](https://avatars.discourse-cdn.com/v4/letter/g/439d5e/32.png) [@Guest2](https://rubytalk.org/u/Guest2)\
**Post date:** [21 November 2006 07:06 UTC](https://rubytalk.org/t/array-question/33112/17 "2006-11-21T07:06:17Z")

</div>

J. B. Rainsberger wrote:

> Of course, if we know the items in the array are in ascending order, we  
> can /really/ cheat:
> 
> b = (a\*2).sort[1..-2]
> 
> That's as many as I could think of.

Thank you but elements in the array are in random order.

Li

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Guest2](https://avatars.discourse-cdn.com/v4/letter/g/439d5e/32.png) [@Guest2](https://rubytalk.org/u/Guest2)\
**Post date:** [21 November 2006 16:05 UTC](https://rubytalk.org/t/array-question/33112/18 "2006-11-21T16:05:34Z")

</div>

Thank you all for your invalulabe inputs.

Based on what I understand and my personal preference I choose the  
following scripts.

Li

> **···**
>
> ####  
> array=[1,2,3,4,5]
> 
> array\_new=Array.new  
> array.each {|e| array\_new\<\<[e,e]}
> 
> array\_new.flatten![1..-2].each{|e| print "#{e}\t" }
> 
> puts
> 
> ##output
> 
> > ruby sur3.rb
> 
> 1 2 2 3 3 4 4 5
> 
> > Exit code: 0
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [22 November 2006 18:10 UTC](https://rubytalk.org/t/array-question/33112/19 "2006-11-22T18:10:35Z")

</div>

> Robert Klemme 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]
> 
> This demonstrates an excellent understanding of inject and

Thank you!

> is a good way to eliminate the somewhat ugly [1..-2].

Well, you can use [1...-1] instead. 🙂

> Here's a prolix way of avoiding array indexing while using zip:
> 
> copy=arr.dup; copy.shift; copy.pop  
> arr.zip(copy).flatten.compact

You can copy and reduce in one step:

\>\> arr=(1..10).to\_a  
=\> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  
\>\> arr.zip(arr[1...-1]).flatten.compact  
=\> [1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10]

Or, more efficient

\>\> arr.zip(arr[1...-1]).flatten!.compact!  
=\> [1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10]

Cheers

&nbsp;&nbsp;robert

> **···**
>
> On 21.11.2006 10:16, William James wrote:
> 
> > > On 21.11.2006 08:18, William James wrote:

---

<div class="post-metadata">

**Author:** ![Edwin\_Fine](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@Edwin\_Fine](https://rubytalk.org/u/Edwin_Fine)\
**Post date:** [21 November 2006 17:34 UTC](https://rubytalk.org/t/array-question/33112/20 "2006-11-21T17:34:01Z")

</div>

Li Chen wrote:

> Thank you all for your invalulabe inputs.
> 
> Based on what I understand and my personal preference I choose the  
> following scripts.
> 
> Li
> 
> ####  
> array=[1,2,3,4,5]
> 
> array\_new=Array.new  
> array.each {|e| array\_new\<\<[e,e]}
> 
> array\_new.flatten![1..-2].each{|e| print "#{e}\t" }
> 
> puts
> 
> ##output
> 
> > ruby sur3.rb
> 
> 1 2 2 3 3 4 4 5
> 
> > Exit code: 0

This is the best I could come up with. It works with any kind of element  
including embedded arrays.

array\_new = (temp = array[1..-2]).zip(temp).inject(){|n, e|  
n.concat(e) }

irb(main):148:0\> array=[1,[2,3],4,5,{6,7},"8"]  
=\> [1, [2, 3], 4, 5, {6=\>7}, "8"]  
irb(main):149:0\> (temp = array[1..-2])  
=\> [[2, 3], 4, 5, {6=\>7}]  
irb(main):150:0\> (temp = array[1..-2]).zip(temp)  
=\> [[[2, 3], [2, 3]], [4, 4], [5, 5], [{6=\>7}, {6=\>7}]]  
irb(main):151:0\> (temp = array[1..-2]).zip(temp).inject() {|new, elem|  
new.concat(elem) }  
=\> [[2, 3], [2, 3], 4, 4, 5, 5, {6=\>7}, {6=\>7}]

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

[Next page](https://rubytalk.org/t/array-question/33112.md?page=2)
