Forum: Ruby Array.join for nested arrays

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0bdb9e1d8ea8709fd4c39322ae8301a3?d=identicon&s=25 unknown (Guest)
on 2007-06-28 20:42
(Received via mailing list)
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),
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|
   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?
Ae16cb4f6d78e485b04ce1e821592ae5?d=identicon&s=25 Martin DeMello (Guest)
on 2007-06-28 21:19
(Received via mailing list)
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?

Here's the most straightforward way:

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

martin
0bdb9e1d8ea8709fd4c39322ae8301a3?d=identicon&s=25 fu (Guest)
on 2007-06-28 21:51
(Received via mailing list)
Martin, thanks! Should the behavior of "join" be like this or the docs
are wrong?
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-06-28 22:30
(Received via mailing list)
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
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-06-29 00:25
(Received via mailing list)
On Jun 28, 2007, at 4:29 PM, Robert Dober wrote:
> irb(main):008:0>  a.join(",")
> -- Kent Back
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
Rob@AgileConsultingLLC.com
Ce8b03e5750097942c58e12b46724312?d=identicon&s=25 Giles Bowkett (Guest)
on 2007-06-29 00:48
(Received via mailing list)
> 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
Portfolio: http://www.gilesgoatboy.org
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-06-29 06:46
(Received via mailing list)
>
> Blog: http://gilesbowkett.blogspot.com
> Portfolio: 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.
-
Ce8b03e5750097942c58e12b46724312?d=identicon&s=25 Giles Bowkett (Guest)
on 2007-06-29 19:53
(Received via mailing list)
> > > 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
Portfolio: http://www.gilesgoatboy.org
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-06-29 21:11
(Received via mailing list)
On 6/29/07, Giles Bowkett <gilesb@gmail.com> wrote:
> 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 ;)
This topic is locked and can not be replied to.