From: Robert Klemme Date: 2003-02-04T23:13:31+09:00 Subject: Re: Mismatch between documenation and implementation of Array#join() "Gavin Sinclair" schrieb im Newsbeitrag news:186173637337.20030205003517@soyabean.com.au... > 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: [...] > > 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