diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2014-07-30 22:48:09 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2014-07-30 22:48:09 -0700 |
commit | 316b32d19c5b31feef53f4cc772870e81a5a909d (patch) | |
tree | 2d08e6a4c44443201d3fe1c5f8b41aa6756b313d | |
parent | 29a6a17a11cd98f18ad46e882cc8f7fd669de59f (diff) | |
parent | acac631cc9a8a0b72e8daf6ab8ad4b1f6f0667bf (diff) | |
download | rails-316b32d19c5b31feef53f4cc772870e81a5a909d.tar.gz rails-316b32d19c5b31feef53f4cc772870e81a5a909d.tar.bz2 rails-316b32d19c5b31feef53f4cc772870e81a5a909d.zip |
Merge pull request #16355 from xaviershay/validate-in-groups-of-args
Raise a descriptive error if non-positive integer passed to in_groups_of
-rw-r--r-- | activesupport/lib/active_support/core_ext/array/grouping.rb | 5 | ||||
-rw-r--r-- | activesupport/test/core_ext/array/grouping_test.rb | 6 |
2 files changed, 11 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/array/grouping.rb b/activesupport/lib/active_support/core_ext/array/grouping.rb index 3529d57174..87ae052eb0 100644 --- a/activesupport/lib/active_support/core_ext/array/grouping.rb +++ b/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 diff --git a/activesupport/test/core_ext/array/grouping_test.rb b/activesupport/test/core_ext/array/grouping_test.rb index b8cfe9728c..2eb0f05141 100644 --- a/activesupport/test/core_ext/array/grouping_test.rb +++ b/activesupport/test/core_ext/array/grouping_test.rb @@ -90,6 +90,12 @@ class GroupingTest < ActiveSupport::TestCase 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 |