DEV Community

Cover image for Ruby Array Methods That Will Blow Your Mind, Pt. 3: Lord of the Slices
Repiki
Repiki

Posted on

Ruby Array Methods That Will Blow Your Mind, Pt. 3: Lord of the Slices

It's all about slicing in this part and we all know at some stage in your development journey, you will need to split an array in various forms. We went through some already but even some more won't be bad.πŸ«₯πŸ˜‰πŸ«₯

slice

The slice method returns a portion of the array based on the provided indices. It does not modify the original collection but instead creates a new one that contains the specified elements. The indices can be provided as a start and length, or as a range.

array = [1, 2, 3, 4, 5]
array.slice(1, 3) 
# Output: [2, 3, 4]

array.slice(0..2)  
# Output: [1, 2, 3]
Enter fullscreen mode Exit fullscreen mode

In the first example, the slice method starts at index 1 and takes 3 elements. In the second example, it takes the elements from index 0 to 2. Note that the original array remains unchanged.

slice_before

The slice_before method is used to group consecutive elements based on a given block. It slices the array before a given element, creating an array of arrays based on the specified condition. This method is particularly useful when you want to split or group elements in a collection based on a certain condition.

array = [1, 2, 3, 4, 5, 6]
sliced_array = array.slice_before { |element| element.even? }
sliced_array.to_a 

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

In the above example, the slice_before method slices the array before every even number. As a result, we get an array of arrays, where each sub-array starts with an even number and includes the following odd numbers.

slice_when

The slice_when method is used to slice the array when the block's condition is true. It creates an array of arrays based on the specified condition. This method is particularly useful when you want to group consecutive elements in a collection based on a certain condition.

numbers = [1, 2, 4, 9, 10, 11, 12, 15, 16, 19, 20]
sliced = numbers.slice_when { |i, j| i+1 != j }
sliced.to_a 

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

In the above example, the slice_when method slices the numbers array when the condition i+1 != j is true. This results in groups of consecutive numbers being grouped together in sub-arrays.

slice_after

The slice_after method is used to slice the array after a given element. It creates an array of arrays based on the specified condition. It slices the collection after a given element.

array = [1, 2, 3, 4, 5, 6]
sliced_array = array.slice_after { |i| i.even? }
sliced_array.to_a 

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

In the above code snippet, the slice_after method is used to slice the array after every even number. The result is an array of arrays, where each sub-array ends with an even number.

Don't stop slicing till next time when we highlight the glue methods just incase you oversliceπŸ˜‰πŸ˜‰.

Top comments (0)