From: Farrel Lifson Date: 2007-02-15T04:26:48+09:00 Subject: Re: Justification for Array#join behaviour? On 14/02/07, Bertram Scharpf wrote: > Hi, > > Am Mittwoch, 14. Feb 2007, 16:52:00 +0900 schrieb Farrel Lifson: > > What is the justification for [1,2,[3,4]].join(',') being '1,2,3,4' > > instead of '1,2,34'? According to ruby-doc.org join "Returns a string > > created by converting each element of the array to a string, separated > > by sep.". Why should an element be recursively joined if it's also an > > array? > > You would have to ask for the objects type: > > class Array > def deep_join sep > map { |x| > case x > when Array then x.deep_join sep > else x.to_s > end > }.join sep > end > end > > I don't look at this as a smart programming style actually. > > The object-oriented approach is to redefine `to_s'. > > Bertram > > > -- > Bertram Scharpf > Stuttgart, Deutschland/Germany > http://www.bertram-scharpf.de > > I did some experimentation and it seems that the decision might not be that half bad. Take for instance a deeply nested array [1,2,[3,4[5,6]]]. Currently calling join produces "1,2,3,4,5,6". Whereas if we used the non-recursive join we'd get "1,2,3456" which actually looks a bit worse. I think it would be better if the documentation was amended to reflect that nested arrays will be flattened. Farrel