On Wed, Aug 6, 2014 at 12:52 AM, Matthew Kerwin <matthew / kerwin.net.au> wrote:
> Another possible expectation could be that it works like this:
>
>   class Array
>     def join sep=$,
>       reduce do |s,e|
>         s.to_s + sep.to_s + e.to_s
>       end.to_s
>     end
>   end

Or, probably a tad more efficient

class Array
  def join2(sep = $,)
    sep = sep.to_s

    reduce nil do |s,e|
      if s
        s << sep << e.to_s
      else
        e.to_s
      end
    end
  end
end

Another way to achieve that would be

irb(main):003:0> [2,[3,4],1,8,[[[5]]]].map(&:to_s).join("|")
=> "2|[3, 4]|1|8|[[[5]]]"

> (Apologies for suboptimal to_s, but you get the idea). Which, depending on
> Array#to_s, could do the following:
>
>   [1,[2,3]].join '|'
>    #=> "1|[2, 3]" in 1.9+, or
>    #   "1|23" in 1.8
>
> Note that the following already happens:
>
>   [1,{2=>3}].join '|'
>    #=> "1|{2=>3}" in 1.9+, or
>    #=> "1|23" in 1.8
>
> And it doesn't matter if you define Hash#join, the recursion only happens if
> the object is_a? Array.
>
> So, again, the documentation is lacking.

I agree.

Kind regards

robert

-- 
[guy, jim].each {|him| remember.him do |as, often| as.you_can - without end}
http://blog.rubybestpractices.com/