Have you look at how Array#to_s is described as returning Array#join (the only difference being that to_s doesn't accept an argument and the default value of the separator is $,)?
irb(main):001:0> a = [1,[2,[3,4],5],6]
=> [1, [2, [3, 4], 5], 6]
irb(main):002:0> a.to_s
=> "123456"
irb(main):003:0> a.join(',')
=> "1,2,3,4,5,6"
irb(main):004:0> $, = '-'
=> "-"
irb(main):005:0> a.to_s
=> "1-2-3-4-5-6"
irb(main):006:0> a.join
=> "1-2-3-4-5-6"
irb(main):007:0> a.join(',')
=> "1,2,3,4,5,6"
Hmm, what about that one with the hash for an element? (Remember, $, is still redefined to '-')
irb(main):008:0> b=[1,{2=>42},{3=>[4,5]}]
=> [1, {2=>42}, {3=>[4, 5]}]
irb(main):009:0> b.to_s
=> "1-2-42-3-4-5"
irb(main):010:0> b.join(',')
=> "1,2-42,3-4-5"
Hash#to_s first converts to an array of [key,value] pairs and then converts that to a string using Array#join with the default separator.
It seems that the use of "default" here, means the $, from Array#join(separator=$,), not the argument given to b.join
OK, one more:
irb(main):011:0> b.last[4]=6
=> 6
irb(main):012:0> b
=> [1, {2=>42}, {3=>[4, 5], 4=>6}]
irb(main):013:0> b.join(',')
=> "1,2-42,3-4-5-4-6"
Possibly unexpected, but I don't see it as inconsistent.
-Rob
Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
···
On Jun 28, 2007, at 4:29 PM, Robert Dober wrote:
On 6/28/07, fu <freeunli@yahoo.com> wrote:
Martin, thanks! Should the behavior of "join" be like this or the docs
are wrong?
Very surprised about this behavior, IMHO it is wrong, at least it is
inconsistent :
irb(main):007:0> a=[1,[2,[3,4],5],6]
=> [1, [2, [3, 4], 5], 6]
irb(main):008:0> a.join(",")
=> "1,2,3,4,5,6"
irb(main):009:0> b=[1,{2=>42},{3=>[4,5]}]
=> [1, {2=>42}, {3=>[4, 5]}]
irb(main):010:0> b.join(",")
=> "1,242,345"
Robert
--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Back