Mismatch between documenation and implementation of Array#join()

Hi,

I’m wondering whether Array#join() has an error or the documentation is
wrong. Documentation says:
“Returns a string created by converting each element of the array to a
string, separated by aSepString.” This apparently does not include
flatten(). But the behavior seems to be like flatten() was invoked before:

With Ruby version 1.6.7:

irb(main):015:0* a=[[0,1],[22,33]]
[[0, 1], [22, 33]]
irb(main):016:0> a.join(";")
“0;1;22;33"
irb(main):017:0> a.flatten.join(”;")
"0;1;22;33"
irb(main):018:0> [0,1].to_s
"01"
irb(main):019:0>

I would have expected a.join(";") to have returned “01;2233” instead.

Any insights? Thank you!

Kind regards

robert

Robert Klemme wrote:

Hi,

I’m wondering whether Array#join() has an error or the documentation is
wrong. Documentation says:
“Returns a string created by converting each element of the array to a
string, separated by aSepString.” This apparently does not include
flatten(). But the behavior seems to be like flatten() was invoked before:

With Ruby version 1.6.7:

irb(main):015:0* a=[[0,1],[22,33]]
[[0, 1], [22, 33]]
irb(main):016:0> a.join(“;”)
“0;1;22;33”

I would have expected a.join(“;”) to have returned “01;2233” instead.

I seem to think it is the correct behaviour, Array#join is just invoked
in each of the subarrays as well(Recursive behaviour, just as #to_s
calls #to_s on all of the array members), however, your wish might be
fulfilled this way:

a.map { |x| x.to_s }.join(‘;’)

or:

class Array

def join_s(sep=nil)
map {|x| x.to_s}.join(sep)
end
end

a.join_s

Idan.

That’s a bad argument for the “correct behaviour” POV. The doc
doesn’t specify recursive behaviour, so why do you expect it? If you
don’t expect it, isn’t the behaviour wrong, no matter how simply it
can be described?

Array#to_s is actually specified (by ‘ri’) as returning ‘arr.join’,
bringing us full circle.

Gavin

···

On Wednesday, February 5, 2003, 12:27:00 AM, Idan wrote:

Robert Klemme wrote:

Hi,

I’m wondering whether Array#join() has an error or the documentation is
wrong. Documentation says:
“Returns a string created by converting each element of the array to a
string, separated by aSepString.” This apparently does not include
flatten(). But the behavior seems to be like flatten() was invoked before:

With Ruby version 1.6.7:

irb(main):015:0* a=[[0,1],[22,33]]
[[0, 1], [22, 33]]
irb(main):016:0> a.join(“;”)
“0;1;22;33”

I would have expected a.join(“;”) to have returned “01;2233” instead.

I seem to think it is the correct behaviour, Array#join is just invoked
in each of the subarrays as well (Recursive behaviour, just as #to_s
calls #to_s on all of the array members), however, your wish might be
fulfilled this way: […]

“Gavin Sinclair” gsinclair@soyabean.com.au schrieb im Newsbeitrag
news:186173637337.20030205003517@soyabean.com.au…

Robert Klemme wrote:

Hi,

I’m wondering whether Array#join() has an error or the documentation is
wrong. Documentation says:
“Returns a string created by converting each element of the array to a
string, separated by aSepString.” This apparently does not include
flatten(). But the behavior seems to be like flatten() was invoked
before:

With Ruby version 1.6.7:

irb(main):015:0* a=[[0,1],[22,33]]
[[0, 1], [22, 33]]
irb(main):016:0> a.join(“;”)
“0;1;22;33”

I would have expected a.join(“;”) to have returned “01;2233” instead.

I seem to think it is the correct behaviour, Array#join is just invoked
in each of the subarrays as well (Recursive behaviour, just as #to_s
calls #to_s on all of the array members), however, your wish might be
fulfilled this way: […]

That’s a bad argument for the “correct behaviour” POV. The doc
doesn’t specify recursive behaviour, so why do you expect it? If you
don’t expect it, isn’t the behaviour wrong, no matter how simply it
can be described?

Or the documentation is in error. However, I prefer to change the code to
actually match the documentation (see below).

Array#to_s is actually specified (by ‘ri’) as returning ‘arr.join’,
bringing us full circle.

Yeah, but the invocation used in to_s is equivalent to arr.join(“”) .
(Emphasis on empty string)

The problem I see is this: I can achieve the current behavior by issuing
a.flatten.join(“;”) which seems IMHO more appropriate than to use what I
regard as a workaround: a.map { |x| x.to_s }.join(‘;’). Using flatten makes
the recursive behavior explicit while the map solution will surely rise
question marks when people find this uncommented somewhere in code of other
people.

Regards

robert
···

On Wednesday, February 5, 2003, 12:27:00 AM, Idan wrote:

I agree. Array#join presumably doesn’t recurse into other objects (I
hope!), so why should it recurse into arrays?

Gavin

···

On Wednesday, February 5, 2003, 1:13:31 AM, Robert wrote:

[big snip]

Or the documentation is in error. However, I prefer to change the code to
actually match the documentation (see below).

“Gavin Sinclair” gsinclair@soyabean.com.au schrieb im Newsbeitrag
news:140176260519.20030205011900@soyabean.com.au…

[big snip]

Or the documentation is in error. However, I prefer to change the code
to
actually match the documentation (see below).

I agree. Array#join presumably doesn’t recurse into other objects (I
hope!), so why should it recurse into arrays?

Exactly. Can we get a comment from matz or whoever maintains this method?

robert
···

On Wednesday, February 5, 2003, 1:13:31 AM, Robert wrote: