Project

General

Profile

Actions

Bug #2957

closed

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

Added by dkelley (Daniel Kelley) over 14 years ago. Updated over 13 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
1.9.2
Backport:
[ruby-core:28643]

Description

=begin
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 ios. The stream must be
    • opened for writing. If the output record separator ($\)
    • opened for writing. If the output field separator ($,)
    • is not nil, it will be inserted between each object.
    • If the output record separator ($\)
    • is not nil, it will be appended to the output. If no
    • arguments are given, prints $_. Objects that aren't
    • strings will be converted by calling their to_s 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"
    =end

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0