This blog is part of our Ruby 2.4 series.
Enumerable#chunk method can be used on enumerator object to group consecutive items based on the value returned from the block passed to it.
Prior to Ruby 2.4,
passing a block
Enumerable#chunk without block in Ruby 2.4
In Ruby 2.4, we will be able to use chunk without passing block. It just returns the enumerator object which we can use to chain further operations.
Reasons for this change
Let’s take the case of listing consecutive integers in an array of ranges.
We had to use
chunk can’t be called without block.
creates a new enumerator object
which will enumerate
the method passed to it.
In this case the method passed was
With Ruby 2.4,
we can use
chunk method directly
as it does not require
a block to be passed.