# 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:** 16\
**Page:** 2

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [21 November 2006 18:34 UTC](https://rubytalk.org/t/array-question/33112/21 "2006-11-21T18:34:12Z")

</div>

dblack@wobblini.net wrote:

> Hi --
> 
> > > 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.

100% agreement here. The problem is easily solved if you ignore the  
flattening risk:

a = (1..5).to\_a  
p a[0..-2].zip(a[1..-1]).flatten  
&nbsp;&nbsp;&nbsp;# ==\> [1, 2, 2, 3, 3, 4, 4, 5]

The inject solution is a little less elegant and probably much less  
efficient:

p a[0..-2].zip(a[1..-1]).inject{|s,x| s+x}  
&nbsp;&nbsp;&nbsp;# ==\> [1, 2, 2, 3, 3, 4, 4, 5]

Maybe a little more efficient:

p a[0..-2].zip(a[1..-1]).inject{|s,x| s.concat x}

I've gotten out the the habit of using #zip for exactly this reason: you  
have to flatten the output to get what you really want, but then you  
might get something you really don't want.

> **···**
>
> > On Tue, 21 Nov 2006, Daniel N wrote:
> > 
> > > On 11/21/06, Daniel N \<has.sox@gmail.com\> wrote:
> 
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<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 22:20 UTC](https://rubytalk.org/t/array-question/33112/22 "2006-11-22T22:20:31Z")

</div>

Robert Klemme wrote:

> \> 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. 🙂

Ha, ha. Also ugly.

> \> 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]

But it has "[1...-1]"! Indexing with literal numbers is so horrid.

> **···**
>
> > On 21.11.2006 10:16, William James wrote:  
> > \>\> On 21.11.2006 08:18, William James wrote:
> 
> > Cheers
> > 
> > &nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Roseanne\_Zhang](https://avatars.discourse-cdn.com/v4/letter/r/f07891/32.png) [@Roseanne\_Zhang](https://rubytalk.org/u/Roseanne_Zhang)\
**Post date:** [21 November 2006 17:48 UTC](https://rubytalk.org/t/array-question/33112/23 "2006-11-21T17:48:54Z")

</div>

Edwin Fine wrote:

> Li Chen wrote:
> 
> > ####  
> > 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

I don't know why we need flatten, it is flatten before you do it.

[code]  
array\_new=  
array.each {|e| array\_new+=[e,e]} #use \<\< will get the same result  
array\_new = array\_new[1..-2]  
puts [array\_new.join(' ')]  
[/code]

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

---

<div class="post-metadata">

**Author:** ![Lou\_Scoras](https://avatars.discourse-cdn.com/v4/letter/l/ee59a6/32.png) [@Lou\_Scoras](https://rubytalk.org/u/Lou_Scoras)\
**Post date:** [21 November 2006 17:51 UTC](https://rubytalk.org/t/array-question/33112/24 "2006-11-21T17:51:38Z")

</div>

I love Enumerator so much =)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require 'enumerator'

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class Array  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def duplicate\_internal  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;to\_enum(:each\_cons,2).inject([]) do |r, c|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r + c  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p [1,2,3,4].duplicate\_internal # =\> [1, 2, 2, 3, 3, 4]

> **···**
>
> --  
> Lou.

---

<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:55 UTC](https://rubytalk.org/t/array-question/33112/25 "2006-11-21T17:55:36Z")

</div>

And a more efficient solution (thanks to Reid Morrison) is

array\_new = (1..array.length - 2).inject([]) {|arr, i| arr \<\< array[i]  
\<\< array[i]}

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

---

<div class="post-metadata">

**Author:** ![Pete\_Yandell](https://avatars.discourse-cdn.com/v4/letter/p/a183cd/32.png) [@Pete\_Yandell](https://rubytalk.org/u/Pete_Yandell)\
**Post date:** [21 November 2006 22:36 UTC](https://rubytalk.org/t/array-question/33112/26 "2006-11-21T22:36:31Z")

</div>

I'm surprised nobody has suggested:

&nbsp;&nbsp;&nbsp;a.zip(a).flatten[1..-2]

Cheers,

Pete Yandell

---

<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:** [22 November 2006 23:05 UTC](https://rubytalk.org/t/array-question/33112/27 "2006-11-22T23:05:55Z")

</div>

I benchmarked the various suggestions and added some of my own, and the  
results may surprise some (they surprised me!)

Rehearsal --------------------------------------------------------------  
:zip\_flatten\_technique 8.280000 0.000000 8.280000 ( 19.532383)  
:enum\_cons\_technique 18.190000 0.020000 18.210000 ( 28.552418)  
:copy\_inject\_technique 5.500000 0.000000 5.500000 ( 5.567597)  
:fill\_technique 5.250000 0.010000 5.260000 ( 5.373526)  
:naive\_technique 5.040000 0.000000 5.040000 ( 5.091017)  
:flatten\_compact\_technique 7.900000 0.000000 7.900000 ( 7.938109)  
:original\_technique 2.560000 0.000000 2.560000 ( 2.620933)  
:zip\_concat\_technique 6.480000 0.000000 6.480000 ( 6.493874)  
---------------------------------------------------- total: 59.230000sec

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
:zip\_flatten\_technique 7.500000 0.000000 7.500000 ( 7.636382)  
:enum\_cons\_technique 17.790000 0.680000 18.470000 ( 18.536960)  
:copy\_inject\_technique 5.250000 0.000000 5.250000 ( 5.277144)  
:fill\_technique 5.070000 0.010000 5.080000 ( 5.165221)  
:naive\_technique 5.100000 0.010000 5.110000 ( 5.113858)  
:flatten\_compact\_technique 7.330000 0.000000 7.330000 ( 7.350595)  
:original\_technique 2.590000 0.000000 2.590000 ( 2.600783)  
:zip\_concat\_technique 6.190000 0.000000 6.190000 ( 6.197641)

So the OP's technique, if not the prettiest, is actually by far the  
fastest, and is correct when used with nested arrays. Some of the other  
solutions (that use flatten) are incorrect when used with nested arrays.

Here's the code:

require 'benchmark'  
require 'enumerator'

techniques = {  
&nbsp;&nbsp;:enum\_cons\_technique =\> lambda {|arr|  
arr.enum\_cons(2).inject([]) {|a, i| a+i}},  
&nbsp;&nbsp;:zip\_flatten\_technique =\> lambda {|arr|  
arr.zip(arr).flatten[1..-2]},  
&nbsp;&nbsp;:copy\_inject\_technique =\> lambda {|arr| copy = [];  
arr.inject{|a,b| copy \<\< a \<\< b; b}; copy},  
&nbsp;&nbsp;:flatten\_compact\_technique =\> lambda {|arr|  
arr.zip(arr[1...-1]).flatten!.compact!},  
&nbsp;&nbsp;:zip\_concat\_technique =\> lambda {|arr| arr.zip(arr).inject([])  
{|n, e| n.concat(e)}[1..-2]},  
&nbsp;&nbsp;:fill\_technique =\> lambda {|arr| (Array.new((arr.length \<\<  
1) - 2) {|i| arr[i \>\> 1]})[1..-2]},  
&nbsp;&nbsp;:naive\_technique =\> lambda do |arr|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a = Array.new((arr.length \* 2) - 2)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[0] = arr.first  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs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\_new  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
}

n = 5000  
array = ["a", 2, (0..1), /^$/, 1.2345, ["arr"] ] \* 100

Benchmark.bmbm do |x|  
&nbsp;&nbsp;techniques.each do |name, proc|  
&nbsp;&nbsp;&nbsp;&nbsp;x.report(name.inspect) { n.times { proc.call(array) } }  
&nbsp;&nbsp;end  
end

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

---

<div class="post-metadata">

**Author:** ![ao\_tianlong1](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@ao\_tianlong1](https://rubytalk.org/u/ao_tianlong1)\
**Post date:** [29 November 2006 05:31 UTC](https://rubytalk.org/t/array-question/33112/28 "2006-11-29T05:31:22Z")

</div>

> Robert Klemme wrote:  
> \> \> 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. 🙂
> 
> Ha, ha. Also ugly.
> 
> \>  
> \> \> 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]
> 
> But it has "[1...-1]"! Indexing with literal numbers is so horrid.
> 
> \>  
> \> Cheers  
> \>  
> \> robert

your name looks like a chinese , and i'm too.

> **···**
>
> 2006/11/23, William James \<w\_a\_x\_man@yahoo.com\>:
> 
> > \> On 21.11.2006 10:16, William James wrote:  
> > \> \>\> On 21.11.2006 08:18, William James wrote:
> > 
> > what about this code ?
> > 
> > irb(main):008:0\> %w{a b c d e f g h i j k l m n o p q r s t u v w x y  
> > z}.map{|i| i = [i,i]}.flatten[1..-2]  
> > =\> ["a", "b", "b", "c", "c", "d", "d", "e", "e", "f", "f", "g", "g",  
> > "h", "h", "i", "i", "j", "j", "k", "k", "l", "l", "m", "m", "n", "n",  
> > "o", "o", "p", "p", "q", "q", "r", "r", "s", "s", "t", "t", "u", "u",  
> > "v", "v", "w", "w", "x", "x", "y", "y", "z"]

---

<div class="post-metadata">

**Author:** ![Lou\_Scoras](https://avatars.discourse-cdn.com/v4/letter/l/ee59a6/32.png) [@Lou\_Scoras](https://rubytalk.org/u/Lou_Scoras)\
**Post date:** [21 November 2006 18:08 UTC](https://rubytalk.org/t/array-question/33112/29 "2006-11-21T18:08:53Z")

</div>

Need to take care of size == 1

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class Array  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def duplicate\_internal  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self if size == 1 # added  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;to\_enum(:each\_cons,2).inject([]) do |r, c|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r + c  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end

> **···**
>
> --  
> Lou.

---

<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 22:53 UTC](https://rubytalk.org/t/array-question/33112/30 "2006-11-21T22:53:20Z")

</div>

Hi --

> **···**
>
> On Wed, 22 Nov 2006, Pete Yandell wrote:
> 
> > I'm surprised nobody has suggested:
> > 
> > a.zip(a).flatten[1..-2]
> 
> I decided to avoid flatten (see my earlier comments). To be  
> flatten-safe, you could do:
> 
> &nbsp;&nbsp;&nbsp;require 'flattenx' # [1]  
> &nbsp;&nbsp;&nbsp;a.zip(a).flatten\_once[1..-2]
> 
> David
> 
> [1] [http://www.rubypal.com/ruby/flattenx](http://www.rubypal.com/ruby/flattenx)
> 
> --  
> &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:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [22 November 2006 01:28 UTC](https://rubytalk.org/t/array-question/33112/31 "2006-11-22T01:28:53Z")

</div>

Pete Yandell [[mailto:pete.yandell@gmail.com](mailto:pete.yandell@gmail.com)] :  
# I'm surprised nobody has suggested:  
# a.zip(a).flatten[1..-2]

indeed. that's a nice surprise. thanks for the tip.  
kind regards -botp

# Cheers,  
# Pete Yandell

> **···**
>
> #  
> #

---

<div class="post-metadata">

**Author:** ![Daniel\_Schierbeck](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daniel\_Schierbeck](https://rubytalk.org/u/Daniel_Schierbeck)\
**Post date:** [22 November 2006 14:54 UTC](https://rubytalk.org/t/array-question/33112/32 "2006-11-22T14:54:24Z")

</div>

I'm surprised nobody has suggested

&nbsp;&nbsp;require 'enumerator'

&nbsp;&nbsp;ary.enum\_cons(2).entries.flatten

Cheers,  
Daniel

> **···**
>
> On Wed, 2006-11-22 at 07:36 +0900, Pete Yandell wrote:
> 
> > I'm surprised nobody has suggested:
> > 
> > &nbsp;&nbsp;&nbsp;a.zip(a).flatten[1..-2]

---

<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:** [22 November 2006 23:19 UTC](https://rubytalk.org/t/array-question/33112/33 "2006-11-22T23:19:56Z")

</div>

> So the OP's technique, if not the prettiest, is actually by far the  
> fastest, and is correct when used with nested arrays. Some of the other  
> solutions (that use flatten) are incorrect when used with nested arrays.

What is meaning for OP here?

Thanks,

Li

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

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://rubytalk.org/u/Martin_DeMello)\
**Post date:** [22 November 2006 18:27 UTC](https://rubytalk.org/t/array-question/33112/34 "2006-11-22T18:27:13Z")

</div>

ary.enum\_cons(2).inject() {|a, i| a+i}

martin

> **···**
>
> On 11/22/06, Daniel Schierbeck \<daniel.schierbeck@gmail.com\> wrote:
> 
> > On Wed, 2006-11-22 at 07:36 +0900, Pete Yandell wrote:  
> > \> I'm surprised nobody has suggested:  
> > \>  
> > \> a.zip(a).flatten[1..-2]
> > 
> > I'm surprised nobody has suggested
> > 
> > &nbsp;&nbsp;require 'enumerator'
> > 
> > &nbsp;&nbsp;ary.enum\_cons(2).entries.flatten

---

<div class="post-metadata">

**Author:** ![Mike\_Stok1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/mike_stok1/32/2065_2.png) [@Mike\_Stok1](https://rubytalk.org/u/Mike_Stok1)\
**Post date:** [22 November 2006 23:22 UTC](https://rubytalk.org/t/array-question/33112/35 "2006-11-22T23:22:48Z")

</div>

Original Poster

> **···**
>
> On 22-Nov-06, at 6:19 PM, Li Chen wrote:
> 
> > > So the OP's technique, if not the prettiest, is actually by far the  
> > > fastest, and is correct when used with nested arrays. Some of the other  
> > > solutions (that use flatten) are incorrect when used with nested arrays.
> > 
> > What is meaning for OP here?
> 
> --
> 
> Mike Stok \<mike@stok.ca\>  
> [http://www.stok.ca/~mike/](http://www.stok.ca/~mike/)
> 
> The "`Stok' disclaimers" apply.

---

<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:** [22 November 2006 23:24 UTC](https://rubytalk.org/t/array-question/33112/36 "2006-11-22T23:24:02Z")

</div>

Li Chen wrote:

> > So the OP's technique, if not the prettiest, is actually by far the  
> > fastest, and is correct when used with nested arrays. Some of the other  
> > solutions (that use flatten) are incorrect when used with nested arrays.
> 
> What is meaning for OP here?
> 
> Thanks,
> 
> Li

Sorry, OP = original poster (that would be YOU 🙂

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

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