Different behavior of '$,' output separator in Ruby 1.9

Discussion in 'Ruby' started by Alex DeCaria, Mar 5, 2010.

  1. Alex DeCaria

    Alex DeCaria Guest

    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/.
     
    Alex DeCaria, Mar 5, 2010
    #1
    1. Advertising

  2. On 3/5/10, Alex DeCaria <> 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.
     
    Caleb Clausen, Mar 5, 2010
    #2
    1. Advertising

  3. 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/.
     
    David Springer, Mar 5, 2010
    #3
  4. On 03/05/2010 08:46 PM, Caleb Clausen wrote:
    > On 3/5/10, Alex DeCaria <> 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@fussel:~$ ruby -e '$,="@";$\="NL\n";print 1,2,3'
    1@2@3NL
    robert@fussel:~$

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Mar 6, 2010
    #4
  5. Alex DeCaria

    Alex DeCaria Guest

    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/.
     
    Alex DeCaria, Mar 7, 2010
    #5
  6. Robert Klemme, Mar 7, 2010
    #6
  7. On 3/7/10, Alex DeCaria <> wrote:
    > Should I submit a bug report, or do you think it will be eventually
    > noticed on this forum?


    File a bug.
     
    Caleb Clausen, Mar 7, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Johnny Ruin
    Replies:
    5
    Views:
    507
  2. pradeep
    Replies:
    3
    Views:
    823
    cwdjrxyz
    Jun 7, 2007
  3. Smurff
    Replies:
    10
    Views:
    2,470
    Keith Thompson
    Nov 21, 2008
  4. nickname
    Replies:
    7
    Views:
    1,079
    Nobody
    Aug 26, 2009
  5. pradeep
    Replies:
    3
    Views:
    141
    Dr J R Stockton
    Jun 7, 2007
Loading...

Share This Page