Permalink
Browse files

Allow IO#reopen to create files

  • Loading branch information...
1 parent 7773d8a commit 6fceb30c2635edfaf03de68574fb1081ddb2383b Evan Phoenix committed May 5, 2010
Showing with 11 additions and 4 deletions.
  1. +11 −4 kernel/common/io.rb
View
@@ -1390,8 +1390,8 @@ def readpartial(size, buffer = nil)
# f2.readlines[0] #=> "This is line one\n"
# f2.reopen(f1) #=> #<File:testfile>
# f2.readlines[0] #=> "This is line one\n"
- def reopen(other, mode="r+")
- if other.respond_to? :to_io
+ def reopen(other, mode=undefined)
+ if other.respond_to?(:to_io)
flush
if other.kind_of? IO
@@ -1411,12 +1411,19 @@ def reopen(other, mode="r+")
else
flush unless closed?
+ # If a mode isn't passed in, use the mode that the IO is already in.
+ if mode.equal? undefined
+ mode = @mode
+ else
+ mode = IO.parse_mode(mode) & ACCMODE
+ end
+
path = StringValue(other)
- fd = IO.sysopen(path, mode, 0666)
+ fd = IO.sysopen(path, mode | CREAT, 0666)
Errno.handle path if fd < 0
reset_buffering
- IO.setup self, fd, mode
+ IO.setup self, fd
seek 0, SEEK_SET
end

0 comments on commit 6fceb30

Please sign in to comment.