Permalink
Browse files

IO::binwrite handles Hash opts as last argument

  • Loading branch information...
1 parent 2bfb8c1 commit ae986bfb8d52cb9ff2810b238ca6f77709e7b435 Michal Bugno committed Apr 12, 2012
Showing with 18 additions and 5 deletions.
  1. +18 −3 kernel/common/io19.rb
  2. +0 −2 spec/tags/19/ruby/core/io/binwrite_tags.txt
View
@@ -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)
@@ -1,2 +0,0 @@
-fails:IO.binwrite accepts a :mode option
-fails:IO.binwrite raises an error if readonly mode is specified

0 comments on commit ae986bf

Please sign in to comment.