Permalink
Browse files

Merge pull request #8236 from marcandre/use_div

Use div
  • Loading branch information...
fxn committed Nov 16, 2012
2 parents 4a4de56 + 3c4a0b2 commit 832b1e5bd6637165e768c3aa32cdf7496a9d96e9
@@ -58,7 +58,7 @@ def in_groups(number, fill_with = nil)
# 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 @@ def in_groups(number, fill_with = nil)
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
@@ -112,6 +112,14 @@ def test_to_s_db
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|

0 comments on commit 832b1e5

Please sign in to comment.