Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Different behavior of '$,' output separator in Ruby 1.9

Reply
Thread Tools

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

 
 
Alex DeCaria
Guest
Posts: n/a
 
      03-05-2010
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/.

 
Reply With Quote
 
 
 
 
Caleb Clausen
Guest
Posts: n/a
 
      03-05-2010
On 3/5/10, Alex DeCaria <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
 
David Springer
Guest
Posts: n/a
 
      03-05-2010
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/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      03-06-2010
On 03/05/2010 08:46 PM, Caleb Clausen wrote:
> On 3/5/10, Alex DeCaria <(E-Mail Removed)> 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/
 
Reply With Quote
 
Alex DeCaria
Guest
Posts: n/a
 
      03-07-2010
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/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      03-07-2010
On 03/07/2010 01:09 PM, Alex DeCaria wrote:
> Should I submit a bug report, or do you think it will be eventually
> noticed on this forum?


I'd check whether that bug is known already and only file in cast it is not:
http://redmine.ruby-lang.org/projects/show/ruby-191

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
Reply With Quote
 
Caleb Clausen
Guest
Posts: n/a
 
      03-07-2010
On 3/7/10, Alex DeCaria <(E-Mail Removed)> wrote:
> Should I submit a bug report, or do you think it will be eventually
> noticed on this forum?


File a bug.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to change stdin separator (perl vs ruby) Sak .. Ruby 5 02-10-2010 04:19 PM
os.popen output different from native shell output nickname Python 7 08-26-2009 02:18 PM
list of html code same but output is different on different browsers pradeep HTML 3 06-07-2007 05:54 PM
Does Ruby need a "line separator" class? Wes Gamble Ruby 9 07-31-2006 07:18 PM
different behavior on different machines, same JRE/IE Johnny Ruin Java 5 03-15-2006 08:51 PM



Advertisments