diff options
author | Xavier Noria <fxn@hashref.com> | 2012-11-16 01:44:40 -0800 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2012-11-16 01:44:40 -0800 |
commit | 832b1e5bd6637165e768c3aa32cdf7496a9d96e9 (patch) | |
tree | a185d01e1efcf4f561ba7260d3b0acd135643523 /activesupport | |
parent | 4a4de567b45ff28035419bc2d92f9b206e3c0a66 (diff) | |
parent | 3c4a0b2e6807cfb67d4ee76a15c8b8a242f12bdf (diff) | |
download | rails-832b1e5bd6637165e768c3aa32cdf7496a9d96e9.tar.gz rails-832b1e5bd6637165e768c3aa32cdf7496a9d96e9.tar.bz2 rails-832b1e5bd6637165e768c3aa32cdf7496a9d96e9.zip |
Merge pull request #8236 from marcandre/use_div
Use div
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/core_ext/array/grouping.rb | 8 | ||||
-rw-r--r-- | activesupport/test/core_ext/array_ext_test.rb | 8 |
2 files changed, 12 insertions, 4 deletions
diff --git a/activesupport/lib/active_support/core_ext/array/grouping.rb b/activesupport/lib/active_support/core_ext/array/grouping.rb index f79b100b3b..640e6e9328 100644 --- a/activesupport/lib/active_support/core_ext/array/grouping.rb +++ b/activesupport/lib/active_support/core_ext/array/grouping.rb @@ -58,7 +58,7 @@ class Array # size / number gives minor group size; # size % number gives how many objects need extra accommodation; # each group hold either division or division + 1 items. - division = size / number + division = size.div number modulo = size % number # create a new array avoiding dup @@ -67,9 +67,9 @@ class Array number.times do |index| length = division + (modulo > 0 && modulo > index ? 1 : 0) - padding = fill_with != false && - modulo > 0 && length == division ? 1 : 0 - groups << slice(start, length).concat([fill_with] * padding) + groups << last_group = slice(start, length) + last_group << fill_with if fill_with != false && + modulo > 0 && length == division start += length end diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb index 9dfa2cbf11..efa7582ab0 100644 --- a/activesupport/test/core_ext/array_ext_test.rb +++ b/activesupport/test/core_ext/array_ext_test.rb @@ -112,6 +112,14 @@ class ArrayExtToSTests < ActiveSupport::TestCase end class ArrayExtGroupingTests < ActiveSupport::TestCase + def setup + Fixnum.send :private, :/ # test we avoid Integer#/ (redefined by mathn) + end + + def teardown + Fixnum.send :public, :/ + end + def test_in_groups_of_with_perfect_fit groups = [] ('a'..'i').to_a.in_groups_of(3) do |group| |