Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

IO::binwrite accepts Hash opts #1663

Merged
merged 4 commits into from

3 participants

@michalbugno

Fix missing IO::bin write functionality to handle Hash as last argument

@dbussink
Owner

Why do you use two different approaches in write and binwrite to parsing the arguments?

@michalbugno

It's either this or quite a lot of duplication in ::write and ::binwrite functionality (you can see the change of last commit). The duplication comes from the fact that apis are quite flexible and allow for example omitting offset.

@dbussink
Owner

It's probably best not to write one with the other. I think I prefer the solution before the last commit you made, seems clearer to me, even though there is more duplication then.

@michalbugno

No probs, I'll rewrite the commits

@michalbugno

Rebased and removed that last commit

@travisbot

This pull request passes (merged f726de9 into 5ea8738).

@dbussink dbussink merged commit de124d3 into rubinius:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
40 kernel/common/io19.rb
@@ -24,19 +24,43 @@ 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
- File.open(file, mode, :encoding => "ASCII-8BIT") do |f|
+ def self.binwrite(file, string, *args)
+ 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 | File::BINARY
+ mode |= File::TRUNC unless offset
+ end
+ 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
View
10 spec/ruby/core/io/shared/binwrite.rb
@@ -55,6 +55,11 @@
File.read(@filename).should == "hello, world!3456789hello, world!"
end
+ it "doesn't truncate and writes at the given offset after passing empty opts" do
+ IO.send(@method, @filename, "hello world!", 1, {})
+ File.read(@filename).should == "0hello world!34567890123456789"
+ end
+
it "accepts a :mode option" do
IO.send(@method, @filename, "hello, world!", :mode => 'a')
File.read(@filename).should == "012345678901234567890123456789hello, world!"
@@ -66,4 +71,9 @@
lambda { IO.send(@method, @filename, "abcde", :mode => "r") }.should raise_error(IOError)
end
+ it "truncates if empty :opts provided and offset skipped" do
+ IO.send(@method, @filename, "hello, world!", {})
+ File.read(@filename).should == "hello, world!"
+ end
+
end
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
View
3  spec/tags/19/ruby/core/io/write_tags.txt
@@ -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
Something went wrong with that request. Please try again.