On Jun 28, 2007, at 4:29 PM, Robert D. wrote:
irb(main):008:0> a.join(“,”)
– Kent Back
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 B. http://agileconsultingllc.com
[email protected]