From: Benoit Daloze Date: 2010-03-01T22:33:51+09:00 Subject: Re: Array#join using to_a if defined --0016e65b63c67a10ac0480bd4cf4 Content-Type: text/plain; charset=ISO-8859-1 On 1 March 2010 04:25, Josh Cheek wrote: > On Sun, Feb 28, 2010 at 12:53 PM, Benoit Daloze > wrote: > > > Hi, > > > > I got recently an unexpected result using Array#join. > > > > In my thoughts, Array#join is sth like adding the separator between every > > element#to_s. > > > > That is what seems to happen when there is no any #to_a method defined in > > an > > element: > > > > For example, > > class N < Struct.new(:n) > > def to_s > > 'to_s' > > end > > end > > > > [N.new(2), N.new(3)].join(' ') #=> "2 3" and not "to_s to_s" > > > > I got "to_s to_s" in 1.8.6, 1.8.7, and 1.9.1 > Me: irb for ruby-1.9.2-r26764 > class N < Struct.new(:n) > def to_s > 'to_s' > end > end => nil > [N.new(2), N.new(3)].join(' ') => "2 3" irb for ruby-1.8.7-p249 > class N < Struct.new(:n) > def to_s > 'to_s' > end > end => nil > [N.new(2), N.new(3)].join(' ') => "to_s to_s" So this would be only 1.9.2 behavior, but why on world this changed? It has such bad side-effect in this case. --- Also, while playing with Array, using Array#&, I saw it look on #eql? and #hash methods. These two methods are relative to Hash usually, I didn't expect methods of Array to depends of them. A surprising result to me: irb for ruby-1.9.2-r26764 > [1,2]&[2] => [2] > class N > def initialize(n) > @n = n > end > attr_reader :n > def == o > @n == o.n > end > end => nil > [N.new(1),N.new(2)]&[N.new(2)] => [] # instead of [#] --0016e65b63c67a10ac0480bd4cf4--