Array.join for nested arrays

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?

On 6/29/07, [email protected] [email protected] 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

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

On 6/28/07, fu [email protected] 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

On Jun 28, 2007, at 4:29 PM, Robert D. 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 B. http://agileconsultingllc.com
[email protected]

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 :wink:

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.

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

Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org

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 :wink:

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

Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org

On 6/29/07, Giles B. [email protected] 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 :wink: