# Array.join for nested arrays

**URL:** <https://rubytalk.org/t/array-join-for-nested-arrays/38710>\
**Category:** ruby-talk\
**Created:** [28 June 2007 18:40 UTC](https://rubytalk.org/t/array-join-for-nested-arrays/38710 "2007-06-28T18:40:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![fu1](https://avatars.discourse-cdn.com/v4/letter/f/4da419/32.png) [@fu1](https://rubytalk.org/u/fu1)\
**Post date:** [28 June 2007 18:40 UTC](https://rubytalk.org/t/array-join-for-nested-arrays/38710/1 "2007-06-28T18:40:09Z")

</div>

Doing

arr = [1, [2, [3, 4], 5], 6]  
arr.join(',')

in Ruby 1.8.6 produces the following string:

1,2,3,4,5,6

Looking at the docs (e.g. [http://www.noobkit.com/ruby-array-join](http://www.noobkit.com/ruby-array-join)),  
Array.join should work as following:

"Returns a string created by converting each element of the array to a  
string, separated by sep."

Doing the following:

arr.each do |e|  
&nbsp;&nbsp;&nbsp;puts e.to\_s  
end

produces:

1  
2345  
6

Thus, arr.join(',') should be:

1, 2345, 6

Am I missing something? How can I do the above?

---

<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:** [28 June 2007 19:18 UTC](https://rubytalk.org/t/array-join-for-nested-arrays/38710/2 "2007-06-28T19:18:18Z")

</div>

Here's the most straightforward way:

arr = [1, [2, [3, 4], 5], 6]  
a = ""  
arr.each {|i| a \<\< i.to\_s \<\< ","}  
a.chop!

martin

> **···**
>
> On 6/29/07, freeunli@yahoo.com \<freeunli@yahoo.com\> wrote:
> 
> > Thus, arr.join(',') should be:
> > 
> > 1, 2345, 6
> > 
> > Am I missing something? How can I do the above?

---

<div class="post-metadata">

**Author:** ![fu1](https://avatars.discourse-cdn.com/v4/letter/f/4da419/32.png) [@fu1](https://rubytalk.org/u/fu1)\
**Post date:** [28 June 2007 19:50 UTC](https://rubytalk.org/t/array-join-for-nested-arrays/38710/3 "2007-06-28T19:50:10Z")

</div>

Martin, thanks! Should the behavior of "join" be like this or the docs  
are wrong?

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [28 June 2007 20:29 UTC](https://rubytalk.org/t/array-join-for-nested-arrays/38710/4 "2007-06-28T20:29:49Z")

</div>

Very surprised about this behavior, IMHO it is wrong, at least it is  
inconsistent :

irb(main):007:0\> a=[1,[2,[3,4],5],6]  
=\> [1, [2, [3, 4], 5], 6]  
irb(main):008:0\> a.join(",")  
=\> "1,2,3,4,5,6"  
irb(main):009:0\> b=[1,{2=\>42},{3=\>[4,5]}]  
=\> [1, {2=\>42}, {3=\>[4, 5]}]  
irb(main):010:0\> b.join(",")  
=\> "1,242,345"

Robert

> **···**
>
> On 6/28/07, fu \<freeunli@yahoo.com\> wrote:
> 
> > Martin, thanks! Should the behavior of "join" be like this or the docs  
> > are wrong?
> 
> --  
> I always knew that one day Smalltalk would replace Java.  
> I just didn't know it would be called Ruby  
> -- Kent Back

---

<div class="post-metadata">

**Author:** ![Rob\_Biedenharn1](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@Rob\_Biedenharn1](https://rubytalk.org/u/Rob_Biedenharn1)\
**Post date:** [28 June 2007 22:24 UTC](https://rubytalk.org/t/array-join-for-nested-arrays/38710/5 "2007-06-28T22:24:46Z")

</div>

Have you look at how Array#to\_s is described as returning Array#join (the only difference being that to\_s doesn't accept an argument and the default value of the separator is $,)?

irb(main):001:0\> a = [1,[2,[3,4],5],6]  
=\> [1, [2, [3, 4], 5], 6]  
irb(main):002:0\> a.to\_s  
=\> "123456"  
irb(main):003:0\> a.join(',')  
=\> "1,2,3,4,5,6"  
irb(main):004:0\> $, = '-'  
=\> "-"  
irb(main):005:0\> a.to\_s  
=\> "1-2-3-4-5-6"  
irb(main):006:0\> a.join  
=\> "1-2-3-4-5-6"  
irb(main):007:0\> a.join(',')  
=\> "1,2,3,4,5,6"

Hmm, what about that one with the hash for an element? (Remember, $, is still redefined to '-')

irb(main):008:0\> b=[1,{2=\>42},{3=\>[4,5]}]  
=\> [1, {2=\>42}, {3=\>[4, 5]}]  
irb(main):009:0\> b.to\_s  
=\> "1-2-42-3-4-5"  
irb(main):010:0\> b.join(',')  
=\> "1,2-42,3-4-5"

Hash#to\_s first converts to an array of [key,value] pairs and then converts that to a string using Array#join with the default separator.

It seems that the use of "default" here, means the $, from Array#join(separator=$,), not the argument given to b.join

OK, one more:

irb(main):011:0\> b.last[4]=6  
=\> 6  
irb(main):012:0\> b  
=\> [1, {2=\>42}, {3=\>[4, 5], 4=\>6}]  
irb(main):013:0\> b.join(',')  
=\> "1,2-42,3-4-5-4-6"

Possibly unexpected, but I don't see it as inconsistent.

-Rob

Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com)  
Rob@AgileConsultingLLC.com

> **···**
>
> On Jun 28, 2007, at 4:29 PM, Robert Dober wrote:
> 
> > On 6/28/07, fu \<freeunli@yahoo.com\> wrote:
> > 
> > > Martin, thanks! Should the behavior of "join" be like this or the docs  
> > > are wrong?
> > 
> > Very surprised about this behavior, IMHO it is wrong, at least it is  
> > inconsistent :
> > 
> > irb(main):007:0\> a=[1,[2,[3,4],5],6]  
> > =\> [1, [2, [3, 4], 5], 6]  
> > irb(main):008:0\> a.join(",")  
> > =\> "1,2,3,4,5,6"  
> > irb(main):009:0\> b=[1,{2=\>42},{3=\>[4,5]}]  
> > =\> [1, {2=\>42}, {3=\>[4, 5]}]  
> > irb(main):010:0\> b.join(",")  
> > =\> "1,242,345"
> > 
> > Robert  
> > --  
> > I always knew that one day Smalltalk would replace Java.  
> > I just didn't know it would be called Ruby  
> > -- Kent Back

---

<div class="post-metadata">

**Author:** ![Giles\_Bowkett](https://avatars.discourse-cdn.com/v4/letter/g/96bed5/32.png) [@Giles\_Bowkett](https://rubytalk.org/u/Giles_Bowkett)\
**Post date:** [28 June 2007 22:47 UTC](https://rubytalk.org/t/array-join-for-nested-arrays/38710/6 "2007-06-28T22:47:30Z")

</div>

> I always knew that one day Smalltalk would replace Java.  
> I just didn't know it would be called Ruby  
> -- Kent Back

It's Kent Beck, isn't it?

Cool quote btw.

> **···**
>
> --  
> Giles Bowkett
> 
> Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
> Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [29 June 2007 04:45 UTC](https://rubytalk.org/t/array-join-for-nested-arrays/38710/7 "2007-06-29T04:45:43Z")

</div>

> \> I always knew that one day Smalltalk would replace Java.  
> \> I just didn't know it would be called Ruby  
> \> -- Kent Back
> 
> It's Kent Beck, isn't it?
> 
> Cool quote btw.
> 
> --  
> Giles Bowkett
> 
> Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
> Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)

It's Kent, AFAIR Rick got confirmation from Kent himself.  
I recently attributed it to Alain Kay, Rick shook a friendly  
forefinger at me and I felt so bad to decide to change my sig. I have  
seen it before on this list though 😉

BTW Giles I do not remember any mention of the $, change in the doc,  
but agreed that would make it consistent, it is therefore a good way  
to look at that behavior without too much surprise.

Cheers  
R.

> **···**
>
> -

---

<div class="post-metadata">

**Author:** ![Giles\_Bowkett](https://avatars.discourse-cdn.com/v4/letter/g/96bed5/32.png) [@Giles\_Bowkett](https://rubytalk.org/u/Giles_Bowkett)\
**Post date:** [29 June 2007 17:52 UTC](https://rubytalk.org/t/array-join-for-nested-arrays/38710/8 "2007-06-29T17:52:41Z")

</div>

> \> \> I always knew that one day Smalltalk would replace Java.  
> \> \> I just didn't know it would be called Ruby  
> \> \> -- Kent Back  
> \>  
> \> It's Kent Beck, isn't it?  
> \>  
> \> Cool quote btw.

> It's Kent, AFAIR Rick got confirmation from Kent himself.

Sorry, no, I mean Beck not Back.

> I recently attributed it to Alain Kay, Rick shook a friendly  
> forefinger at me and I felt so bad to decide to change my sig. I have  
> seen it before on this list though 😉

Definitely an awesome quote.

> BTW Giles I do not remember any mention of the $, change in the doc,  
> but agreed that would make it consistent, it is therefore a good way  
> to look at that behavior without too much surprise.

Hmm. I don't remember saying what you're agreeing with me about, but  
hey, obviously I must have been right.

> **···**
>
> --  
> Giles Bowkett
> 
> Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
> Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [29 June 2007 19:09 UTC](https://rubytalk.org/t/array-join-for-nested-arrays/38710/9 "2007-06-29T19:09:48Z")

</div>

> \> \> \> I always knew that one day Smalltalk would replace Java.  
> \> \> \> I just didn't know it would be called Ruby  
> \> \> \> -- Kent Back  
> \> \>  
> \> \> It's Kent Beck, isn't it?  
> \> \>  
> \> \> Cool quote btw.
> 
> \> It's Kent, AFAIR Rick got confirmation from Kent himself.
> 
> Sorry, no, I mean Beck not Back.

I hereby declare myself an Agghead!!!!

Thx Giles, quote corrected, I can only hope that Kent is \*not\* reading  
this list!!!

> Hmm. I don't remember saying what you're agreeing with me about, but  
> hey, obviously I must have been right.

Quite obviously 😉

> **···**
>
> On 6/29/07, Giles Bowkett \<gilesb@gmail.com\> wrote:
> 
> --  
> I always knew that one day Smalltalk would replace Java.  
> I just didn't know it would be called Ruby  
> -- Kent Beck
