Issue #5915 has been updated by Marc-Andre Lafortune.


I feel that both Yui and John are right.

As Yui points out, the documentation doesn't state *explicitly* how `nil` is treated.

On the other hand, I agree with John that the current behaviour is not intuitive and that either the documentation should be clarified or the behaviour should be changed.

1) To explicitly describe the current behavior, the documentation needs to state that if `sep` is `nil`, then `$,` is used **and that if `$,` is `nil`, then no separator is used**. This last part is also needed, because currently `Array#join` does not call `#to_s` on `sep`, so `nil` is actually a special value.

Or else:
2) The current behavior so that `sep == nil` means that there will be no separator. Documentation could state this explicitly by saying that "... separated by +sep+ (if non-nil)". Clearly, the shorter documentation reflects the fact that this is simpler. I believe this is what John suggests.

3) The behaviour could also be changed so that `Array#join` calls `#to_s` on `sep`, like it does on its members. Most Ruby functions only call `to_str` on the arguments, but `Array#join` calls `to_s` on the elements of the array, so it's not clear (to me) why it doesn't on `sep` (maybe Matz has an idea?). In that case, the behavior would match what John and I favor, and the documentation doesn't really need to change or could state that "elements and `sep` will be converted to strings if need be by calling `to_s`".

I personally favor (2) or (3), so that:

    $, = "foo"
    [4,2].join(nil) == "42"

Yui, do you prefer the current behavior where the result is "4foo2"?
----------------------------------------
Bug #5915: Array#join with explicit nil should not use $,
https://bugs.ruby-lang.org/issues/5915

Author: John Firebaugh
Status: Open
Priority: Normal
Assignee: 
Category: 
Target version: 
ruby -v: ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin10.8.0]


=begin
The documentation for Array#join says that the default argument is $,. But:

  $, = ","
  [1,2,3].join(nil) #=> "1,2,3"

Based on the documentation (and the POLS), I would expect "123".
=end



-- 
http://bugs.ruby-lang.org/