Permalink
Browse files

IO::write also respects opts (together with :open_args)

  • Loading branch information...
1 parent ae986bf commit 23ead4d16506a757821a14ff03a00510dbad86dd Michal Bugno committed Apr 12, 2012
Showing with 22 additions and 16 deletions.
  1. +22 −13 kernel/common/io19.rb
  2. +0 −3 spec/tags/19/ruby/core/io/write_tags.txt
View
@@ -25,33 +25,42 @@ def self.binread(file, length=nil, offset=0)
end
def self.binwrite(file, string, *args)
- if args.size > 2
- raise ArgumentError, "wrong number of arguments (#{args.size + 2} for 2..3)"
- end
-
offset, opts = args
opts ||= {}
if offset.is_a?(Hash)
offset, opts = nil, offset
end
- if opts[:mode]
- mode = opts[:mode]
- else
+ mode, binary, external, internal = IO.normalize_options(nil, opts)
+ unless mode
mode = File::CREAT | File::RDWR | File::BINARY
mode |= File::TRUNC unless offset
end
-
- File.open(file, mode, :encoding => "ASCII-8BIT") do |f|
+ File.open(file, mode, :encoding => (external || "ASCII-8BIT")) do |f|
f.seek(offset || 0)
f.write(string)
end
end
- def self.write(file, string, offset=nil)
- mode = File::CREAT | File::RDWR
- mode |= File::TRUNC unless offset
- File.open(file, mode, :encoding => "ASCII-8BIT") do |f|
+ def self.write(file, string, *args)
+ if args.size > 2
+ raise ArgumentError, "wrong number of arguments (#{args.size + 2} for 2..3)"
+ end
+
+ offset, opts = args
+ opts ||= {}
+ if offset.is_a?(Hash)
+ offset, opts = nil, offset
+ end
+
+ mode, binary, external, internal = IO.normalize_options(nil, opts)
+ unless mode
+ mode = File::CREAT | File::RDWR
+ mode |= File::TRUNC unless offset
+ end
+
+ open_args = opts[:open_args] || [mode, :encoding => (external || "ASCII-8BIT")]
+ File.open(file, *open_args) do |f|
f.seek(offset || 0)
f.write(string)
end
@@ -1,5 +1,2 @@
-fails:IO.write accepts a :mode option
-fails:IO.write raises an error if readonly mode is specified
fails:IO.write uses encoding from given options, if provided
fails:IO.write uses an :open_args option
-fails:IO.write disregards other options if :open_args is given

0 comments on commit 23ead4d

Please sign in to comment.