Project

General

Profile

Actions

Bug #18957

closed

StringIO#printf doesn't ignore initial String value

Bug #18957: StringIO#printf doesn't ignore initial String value

Added by andrykonchin (Andrew Konchin) over 3 years ago. Updated over 3 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:109430]

Description

I've noticed that StringIO#printf diverges from IO#printf in the following way:

IO:

File.write("printf.txt", "1234567890") f = File.open("printf.txt", "w+") f.printf("%s", "abc") f.rewind f.gets # => "abc" 

StringIO:

io = StringIO.new("1234567890") io.printf("%s", "abc") io.rewind io.gets => "abc4567890" 

So File opened in a w mode "truncates" existing file content but StirngIO (by default) doesn't.

I would expect StringIO behaves in the same way.

Updated by mame (Yusuke Endoh) over 3 years ago Actions #1 [ruby-core:109438]

StringIO behaves like File.open with an r+ mode.

File.write("printf.txt", "1234567890") f = File.open("printf.txt", "r+") f.printf("%s", "abc") f.rewind p f.gets #=> "abc4567890" 

Is there any reason for you to expect it to be the same as w+ mode instead of r+?

Updated by andrykonchin (Andrew Konchin) over 3 years ago Actions #2 [ruby-core:109439]

Good point. I overlooked the difference between r+ and w+. Thank you for clarification.

Updated by jeremyevans0 (Jeremy Evans) over 3 years ago Actions #3

  • Status changed from Open to Rejected
Actions

Also available in: PDF Atom