Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

IO::binwrite handles Hash opts as last argument

  • Loading branch information...
commit ae986bfb8d52cb9ff2810b238ca6f77709e7b435 1 parent 2bfb8c1
Michal Bugno authored
View
21 kernel/common/io19.rb
@@ -24,9 +24,24 @@ def self.binread(file, length=nil, offset=0)
end
end
- def self.binwrite(file, string, offset=nil)
- mode = File::CREAT | File::RDWR | File::BINARY
- mode |= File::TRUNC unless offset
+ 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 = File::CREAT | File::RDWR | File::BINARY
+ mode |= File::TRUNC unless offset
+ end
+
File.open(file, mode, :encoding => "ASCII-8BIT") do |f|
f.seek(offset || 0)
f.write(string)
View
2  spec/tags/19/ruby/core/io/binwrite_tags.txt
@@ -1,2 +0,0 @@
-fails:IO.binwrite accepts a :mode option
-fails:IO.binwrite raises an error if readonly mode is specified
Please sign in to comment.
Something went wrong with that request. Please try again.