Skip to content

Commit

Permalink
Merge pull request #16355 from xaviershay/validate-in-groups-of-args
Browse files Browse the repository at this point in the history
Raise a descriptive error if non-positive integer passed to in_groups_of
  • Loading branch information
tenderlove committed Jul 31, 2014
2 parents 29a6a17 + acac631 commit 316b32d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions activesupport/lib/active_support/core_ext/array/grouping.rb
Expand Up @@ -18,6 +18,11 @@ class Array
# ["3", "4"]
# ["5"]
def in_groups_of(number, fill_with = nil)
if number.to_i <= 0
raise ArgumentError,
"Group size must be a positive integer, was #{number.inspect}"
end

if fill_with == false
collection = self
else
Expand Down
6 changes: 6 additions & 0 deletions activesupport/test/core_ext/array/grouping_test.rb
Expand Up @@ -90,6 +90,12 @@ def test_in_groups_without_padding
assert_equal [[1, 2, 3], [4, 5], [6, 7]],
(1..7).to_a.in_groups(3, false)
end

def test_in_groups_invalid_argument
assert_raises(ArgumentError) { [].in_groups_of(0) }
assert_raises(ArgumentError) { [].in_groups_of(-1) }
assert_raises(ArgumentError) { [].in_groups_of(nil) }
end
end

class SplitTest < ActiveSupport::TestCase
Expand Down

0 comments on commit 316b32d

Please sign in to comment.