Skip to content

Instantly share code, notes, and snippets.

@notahat
Forked from joho/test_array_in_chunks.rb
Created May 21, 2009 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save notahat/115405 to your computer and use it in GitHub Desktop.
Save notahat/115405 to your computer and use it in GitHub Desktop.
require 'test/unit'
require 'enumerator'
class Array
def in_chunks_of(size_of_chunks)
enum_slice(size_of_chunks).to_a
end
end
class TestArrayInChunks < Test::Unit::TestCase
def test_chunking_equal_to_size
assert_equal [1, 2].in_chunks_of(2), [[1, 2]]
end
def test_chunk_greater_than_size
assert_equal [1, 2].in_chunks_of(4), [[1, 2]]
end
def test_even_chunks
assert_equal [1, 2, 3, 4].in_chunks_of(2), [[1, 2], [3, 4]]
end
def test_more_than_two_chunks
assert_equal [1, 2, 3, 4, 5, 6].in_chunks_of(2), [[1, 2], [3, 4], [5, 6]]
end
def test_more_than_two_chunks_with_remainder
assert_equal [1, 2, 3, 4, 5, 6, 7].in_chunks_of(2), [[1, 2], [3, 4], [5, 6], [7]]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment