Skip to content
Browse files

Merge pull request #16355 from xaviershay/validate-in-groups-of-args

Raise a descriptive error if non-positive integer passed to in_groups_of
  • Loading branch information...
2 parents 29a6a17 + acac631 commit 316b32d19c5b31feef53f4cc772870e81a5a909d @tenderlove tenderlove committed Jul 30, 2014
View
5 activesupport/lib/active_support/core_ext/array/grouping.rb
@@ -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
View
6 activesupport/test/core_ext/array/grouping_test.rb
@@ -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

0 comments on commit 316b32d

Please sign in to comment.
Something went wrong with that request. Please try again.