[ruby-core:109438] [Ruby master Bug#18957] StringIO#printf doesn't ignore initial String value
From:
"mame (Yusuke Endoh)" <noreply@...>
Date:
2022-08-06 14:53:42 UTC
List:
ruby-core #109438
Issue #18957 has been updated by mame (Yusuke Endoh).
`StringIO` behaves like `File.open` with an `r+` mode.
```ruby
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+`?
----------------------------------------
Bug #18957: StringIO#printf doesn't ignore initial String value
https://bugs.ruby-lang.org/issues/18957#change-98591
* Author: andrykonchin (Andrew Konchin)
* Status: Open
* Priority: Normal
* ruby -v: 3.0.3
* Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN
----------------------------------------
I've noticed that `StringIO#printf` diverges from `IO#printf` in the following way:
IO:
```ruby
File.write("printf.txt", "1234567890")
f = File.open("printf.txt", "w+")
f.printf("%s", "abc")
f.rewind
f.gets
# => "abc"
```
StringIO:
```ruby
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.
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>