Bug #2957

IO.print emits field separator after each object, rather than between

Added by Daniel Kelley almost 2 years ago. Updated 10 months ago.

[ruby-core:28643]
Status:Closed Start date:03/14/2010
Priority:Normal Due date:
Assignee:- % Done:

100%

Category:core
Target version:-
ruby -v:1.9.2

Description

In versions prior to 1.9, IO.print emits the field separator "$," between each object printed when non-nil.
As of r11003, IO.print emits the field separator after each field. This change was reported by
Alex DeCaria in [ruby-talk:358633].

The following patch
  (a) reverts the behavior to what it was in 1.8.X
  (b) Documents the behavior of the field separator
  (c) Adds a test case

Index: io.c
===================================================================
--- io.c	(revision 26905)
+++ io.c	(working copy)
@@ -5879,7 +5879,9 @@
  *     ios.print(obj, ...)     => nil
  *
  *  Writes the given object(s) to <em>ios</em>. The stream must be
- *  opened for writing. If the output record separator (<code>$\\</code>)
+ *  opened for writing. If the output field separator (<code>$,</code>)
+ *  is not <code>nil</code>, it will be inserted between each object.
+ *  If the output record separator (<code>$\\</code>)
  *  is not <code>nil</code>, it will be appended to the output. If no
  *  arguments are given, prints <code>$_</code>. Objects that aren't
  *  strings will be converted by calling their <code>to_s</code> method.
@@ -5906,10 +5908,10 @@
 	argv = &line;
     }
     for (i=0; i<argc; i++) {
-	rb_io_write(out, argv[i]);
-	if (!NIL_P(rb_output_fs)) {
+	if (!NIL_P(rb_output_fs) && i>0) {
 	    rb_io_write(out, rb_output_fs);
 	}
+	rb_io_write(out, argv[i]);
     }
     if (argc > 0 && !NIL_P(rb_output_rs)) {
 	rb_io_write(out, rb_output_rs);
Index: test/ruby/test_io.rb
===================================================================
--- test/ruby/test_io.rb	(revision 26905)
+++ test/ruby/test_io.rb	(working copy)
@@ -1386,6 +1386,23 @@
     assert_in_out_err(["-", t.path], "print while $<.gets", %w(foo bar baz), [])
   end

+  def test_print_separators
+    $, = ':'
+    $\ = "\n"
+    r, w = IO.pipe
+    w.print('a')
+    w.print('a','b','c')
+    w.close
+    assert_equal("a\n", r.gets)
+    assert_equal("a:b:c\n", r.gets)
+    assert_nil r.gets
+    r.close
+    
+  ensure
+    $, = nil
+    $\ = nil
+  end
+
   def test_putc
     pipe(proc do |w|
       w.putc "A"

Associated revisions

Revision 26938
Added by Yukihiro Matsumoto almost 2 years ago

* io.c (rb_io_print): RDoc update. a patch from Daniel Kelley in [ruby-core:28643].

History

Updated by Daniel Kelley almost 2 years ago

Ok, somebody beat me to the code, great! Documentation and test case is still relevant.

Updated by Yukihiro Matsumoto almost 2 years ago

Hi,

In message "Re: [ruby-core:28644] [Bug #2957] IO.print emits field separator after each object, rather than between"
    on Sun, 14 Mar 2010 00:30:25 +0900, Daniel Kelley <redmine@ruby-lang.org> writes:

|Ok, somebody beat me to the code, great! Documentation and test case is still relevant.

Your documentation patch and test case were merged.  Thank you.

							matz.

Updated by Yukihiro Matsumoto almost 2 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100
This issue was solved with changeset r26938.
Daniel, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.

Also available in: Atom PDF