Skip to content

Commit

Permalink
IO::binwrite handles Hash opts as last argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Bugno committed Apr 21, 2012
1 parent 2bfb8c1 commit ae986bf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
21 changes: 18 additions & 3 deletions kernel/common/io19.rb
Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions spec/tags/19/ruby/core/io/binwrite_tags.txt

This file was deleted.

0 comments on commit ae986bf

Please sign in to comment.