Skip to content

Commit

Permalink
Add Array#slice! positive length check and spec
Browse files Browse the repository at this point in the history
  • Loading branch information
ruipserra committed Feb 20, 2015
1 parent 4417909 commit 7e71609
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion kernel/common/array.rb
Expand Up @@ -1580,7 +1580,8 @@ def slice!(start, length=undefined)
end
else
start = Rubinius::Type.coerce_to_collection_index start
length = Rubinius::Type.coerce_to_collection_index length
length = Rubinius::Type.coerce_to_collection_length length
return nil if length < 0

out = self[start, length]

Expand Down
6 changes: 6 additions & 0 deletions spec/ruby/core/array/slice_spec.rb
Expand Up @@ -39,6 +39,12 @@
a.should == []
end

it "returns nil if length is negative" do
a = [1, 2, 3]
a.slice!(2, -1).should == nil
a.should == [1, 2, 3]
end

it "properly handles recursive arrays" do
empty = ArraySpecs.empty_recursive_array
empty.slice(0).should == empty
Expand Down

0 comments on commit 7e71609

Please sign in to comment.