summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorksss <[email protected]>2017-06-12 09:50:51 +0900
committerksss <[email protected]>2017-06-12 09:57:18 +0900
commit051d7bc8dbfca584789ae8e9c1c393aea3aa72b7 (patch)
tree5cfabd8b2b7d6c0daad43b34be17d652d1b29a6a
parent3a73a5849388fe908af7131804f9fc7f55472651 (diff)
downloadmruby-051d7bc8dbfca584789ae8e9c1c393aea3aa72b7.tar.gz
mruby-051d7bc8dbfca584789ae8e9c1c393aea3aa72b7.zip
Reseek when write
-rw-r--r--mrblib/io.rb4
-rw-r--r--test/io.rb10
2 files changed, 14 insertions, 0 deletions
diff --git a/mrblib/io.rb b/mrblib/io.rb
index 694fdb0c9..3ef9c7b5d 100644
--- a/mrblib/io.rb
+++ b/mrblib/io.rb
@@ -123,6 +123,10 @@ class IO
def write(string)
str = string.is_a?(String) ? string : string.to_s
return str.size unless str.size > 0
+ if 0 < @buf.length
+ # reset real pos ignore buf
+ seek(pos, SEEK_SET)
+ end
len = syswrite(str)
@pos += len
len
diff --git a/test/io.rb b/test/io.rb
index b654ff213..f1dd7afe8 100644
--- a/test/io.rb
+++ b/test/io.rb
@@ -181,6 +181,16 @@ assert('IO#write', '15.2.20.5.20') do
io = IO.open(IO.sysopen($mrbtest_io_wfname))
assert_equal 0, io.write("")
io.close
+
+ io = IO.open(IO.sysopen($mrbtest_io_wfname, "r+"), "r+")
+ assert_equal 7, io.write("abcdefg")
+ io.rewind
+ assert_equal "ab", io.read(2)
+ assert_equal 3, io.write("123")
+ io.rewind
+ assert_equal "ab123fg", io.read
+ io.close
+
true
end