From: Alex DeCaria on 5 Mar 2010 14:06 In switching to Ruby 1.9 from 1.8 I notice that the behavior of the '$,' output separator has changed. In Ruby 1.9 the output separator is placed after a newline character, but this didn't happen with Ruby 1.8. Using the following code: ------------ $, = ', ' data = [['a', 'b', 'c'], ['1', '2', '3'], ['x', 'y', 'z']] file_out = File.new("test.csv", "w") data.each do |elem| file_out.print elem[0], elem[1], elem[2], "\n" end file_out.close --------------- and running with Ruby 1.8 produces a file that contains a, b, c, 1, 2, 3, x, y, z, but with Ruby 1.9 the contents of the file look like a, b, c, , 1, 2, 3, , x, y, z, , Is this an intentional change of behavior? --Alex -- Posted via http://www.ruby-forum.com/.
From: Caleb Clausen on 5 Mar 2010 14:46 On 3/5/10, Alex DeCaria <alex.decaria(a)millersville.edu> wrote: > In switching to Ruby 1.9 from 1.8 I notice that the behavior of the '$,' > output separator has changed. In Ruby 1.9 the output separator is > placed after a newline character, but this didn't happen with Ruby 1.8. That doesn't seem right.
From: David Springer on 5 Mar 2010 14:56 Alex, If you make a few minor modifications: ------------------------------------------- $, = ', ' $\ = "\n" ### output record separator ### data = [['a', 'b', 'c'], ['1', '2', '3'], ['x', 'y', 'z']] file_out = File.new("test.csv", "w") data.each do |elem| file_out.print elem[0], elem[1], elem[2] ### no newline FIELD ### end file_out.close ------------------------------------------- then you will get what you want in Ruby 1.9. a, b, c, 1, 2, 3, x, y, z, I have not tried it in Ruby 1.8. Looks like it is adding the output field separator AFTER the newline FIELD. -- Posted via http://www.ruby-forum.com/.
From: Robert Klemme on 6 Mar 2010 09:27 On 03/05/2010 08:46 PM, Caleb Clausen wrote: > On 3/5/10, Alex DeCaria <alex.decaria(a)millersville.edu> wrote: >> In switching to Ruby 1.9 from 1.8 I notice that the behavior of the '$,' >> output separator has changed. In Ruby 1.9 the output separator is >> placed after a newline character, but this didn't happen with Ruby 1.8. > > That doesn't seem right. I agree. 1.9 writes one field separator too much. This must be a bug. Alex, separators are intended to be used a tad differently. You would rather set the output record separator as well: robert(a)fussel:~$ ruby -e '$,="@";$\="NL\n";print 1,2,3' 1@2(a)3NL robert(a)fussel:~$ Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/
From: Alex DeCaria on 7 Mar 2010 07:09 Should I submit a bug report, or do you think it will be eventually noticed on this forum? --Alex -- Posted via http://www.ruby-forum.com/.
|
Next
|
Last
Pages: 1 2 Prev: CentOS 5 and Ruby 1.9.1 - conflict with 1.8.6 Next: Conditional keys in hash - out of the box? |