From: Jason Foreman Date: 2005-07-29T01:15:46+09:00 Subject: Re: Joining an Array of Arrays On 7/28/05, Jesse Merriman wrote: > Hi, > > I tried joining an Array of Arrays, and it seems the Array > is being flattened first or something. > > ri Array#join tells me: > Returns a string created by converting each element of the array to > a string, separated by _sep_. > > Its the part about each element getting converted to a string that > seems wrong: > > > x = [[1, 2], [3, 4]] > > print x[0].to_s, ' ', x[1].to_s #=> "12 34" > > x.join(' ') #=> "1 2 3 4" > > I expected join to produce "12 34"; why doesn't it? > This boggled me for a while, had to actually look at the source for Array#join. It does not simply call to_s when the sub-element is an array. Essentially, it does a recursive call to join for elements that are themselves arrays, passing in the original separator (' ' in this case). Jason