DEV Community

Cover image for Ruby Array Methods That Will Blow Your Mind, Pt. 2: Splitters Everywhere
Repiki
Repiki

Posted on

Ruby Array Methods That Will Blow Your Mind, Pt. 2: Splitters Everywhere

If you haven't seen the first bit please follow this link so you get the first scoop or you can also carry on with this too and backtrack later.

Getting down straight to the business of the day, showcasing even cooler ruby methods...

partition

I bet you never knew this method existed in ruby😜. Cool yeah!😎. The partition method is used to divide elements of an array into two groups based on a given condition. It returns an array of two arrays. The first array contains elements for which the block evaluates to true, and the second array contains elements for which the block evaluates to false.

numbers = [1, 2, 3, 4, 5, 6]
even_and_odd = numbers.partition { |number| number.even? }

# Output: [[2, 4, 6], [1, 3, 5]]
Enter fullscreen mode Exit fullscreen mode

In the above example, the partition method is used to separate even and odd numbers in the array. The returned array contains two arrays: the first with even numbers and the second with odd numbers.

each_cons

The each_cons method is used to iterate over consecutive elements in the collection. This method allows you to process elements in sliding windows of a specified size. It provides a way to iterate over consecutive elements in the collection.

array = [1, 2, 3, 4, 5]
array.each_cons(2) { |a| p a }

# Output:
# [1, 2]
# [2, 3]
# [3, 4]
# [4, 5]
Enter fullscreen mode Exit fullscreen mode

In the above example, each_cons(2) iterates over the array in groups of two consecutive elements at a time.

NOTE: Do not confuse each_cons with each_slice in Part 1 as they split the array differently.

If you have a sequence of numbers [1, 2, 3, 4, 5], each_cons(2) would yield [[1, 2], [2, 3], [3, 4], [4, 5]], whereas each_slice(2) would yield [[1, 2], [3, 4], [5]].

chunk

The chunk method groups consecutive elements based on a given block. It returns an enumerable that, when iterated over, yields arrays of two elements. The first element is the result of the block for that group of elements, and the second element is an array of those elements. This method is useful when you want to group consecutive elements together based on some shared characteristic.

numbers = [1, 2, 3, 4, 5, 6]
chunked = numbers.chunk { |number| number.even? }

# Output: [[false, [1, 3, 5]], [true, [2, 4, 6]]]

chunked.each { |even, array| puts "#{even ? 'Even' : 'Odd'}: #{array}" }
Enter fullscreen mode Exit fullscreen mode

In this example, the chunk method is used to group the numbers into chunks of consecutive even and odd numbers. The block { |number| number.even? } is used to determine how to group the elements. The output would be "Odd: [1]", "Even: [2]", "Odd: [3]", "Even: [4]", "Odd: [5]", "Even: [6]".

chunk_while

The chunk_while method is used to group consecutive elements based on a given block until the block condition is false. It creates an array of arrays representing chunks of elements for which the block condition was true consecutively.

array = [1, 2, 4, 9, 10, 11, 12, 15, 16, 19, 20]
array.chunk_while { |i, j| i + 1 == j }.to_a

Output: [[1, 2], [4], [9, 10, 11, 12], [15, 16], [19, 20]]
Enter fullscreen mode Exit fullscreen mode

In the above example, chunk_while groups consecutive integers together into sub-arrays. The block i + 1 == j checks if the current element and the next element are consecutive numbers.

Play around with these methods and place them nicely in your ruby toolbox and keep some space as there are more to come.

Top comments (0)