Skip to content

Commit

Permalink
Merge pull request #217 in G/truffleruby from io-read-fix to master
Browse files Browse the repository at this point in the history
* commit 'a336dd6969c7f488bd9167b941138c920a1ae2eb':
  Handle IO.read with length and options but no offset.
  • Loading branch information
eregon committed Jul 19, 2018
2 parents 5ce2fcc + a336dd6 commit a50d912
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 4 additions & 0 deletions spec/ruby/core/io/read_spec.rb
Expand Up @@ -27,6 +27,10 @@
IO.read(@fname, {}).should == @contents
end

it "accepts a length, and empty options Hash" do
IO.read(@fname, 3, {}).should == @contents[0, 3]
end

it "accepts a length, offset, and empty options Hash" do
IO.read(@fname, 3, 0, {}).should == @contents[0, 3]
end
Expand Down
9 changes: 7 additions & 2 deletions src/main/ruby/core/io.rb
Expand Up @@ -635,8 +635,13 @@ def self.read(name, length_or_options=undefined, offset=0, options=nil)
offset = 0
options = length_or_options
elsif length_or_options
offset = Truffle::Type.coerce_to_int(offset || 0)
raise Errno::EINVAL, 'offset must not be negative' if offset < 0
if Truffle::Type.object_kind_of? offset, Hash
options = offset
offset = 0
else
offset = Truffle::Type.coerce_to_int(offset || 0)
raise Errno::EINVAL, 'offset must not be negative' if offset < 0
end

length = Truffle::Type.coerce_to_int(length_or_options)
raise ArgumentError, 'length must not be negative' if length < 0
Expand Down

0 comments on commit a50d912

Please sign in to comment.