DEV Community

Cover image for Ruby Array Methods That Will Blow Your Mind.
Repiki
Repiki

Posted on • Updated on

Ruby Array Methods That Will Blow Your Mind.

It could be a sleek method you use in your day job, your side project or your technical interview but these methods used to process an array in ruby will totally leave you in awe.

all?

Yes asking a question about all the elements of your array.The all? method is used in Ruby to check if all elements in an Enumerable meet a certain condition. It returns true if all elements satisfy the condition, otherwise it returns false. This method is particularly useful when you need to validate that every element in a collection such as an array or hash meets a specific criteria.

numbers = [1, 2, 3, 4, 5]
all_even = numbers.all? { |num| num.even? }
puts all_even
Enter fullscreen mode Exit fullscreen mode

In the above example, the all? method is used to check if all numbers in the array are even. Since not all numbers are even, the output is false

each_slice

Not slices of bread now but slices of your array. Really why process just one element of your array one at a time?

The each_slice method allows you to iterate over an array in slices of a given size. This means that instead of processing each element individually, you can process them in groups of a specified size.

array = [1, 2, 3, 4, 5, 6]
array.each_slice(2) { |slice| p slice }
Enter fullscreen mode Exit fullscreen mode

In the above code snippet, the each_slice method is used to iterate over the array in slices of 2. This will output each slice as an array, resulting in the following output:

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

grep

Oh, you thought grep was only a linux utility? No, it is also available for you in ruby. The grep method is used to filter elements based on a regular expression. It returns an array with elements that match the pattern. This method is not limited to regular expressions, it can also accept a Class, and it will return an array of elements that are instances of that Class.

array = [1, 2, 'three', 'four', 5]
filtered_array = array.grep(String)
# filtered_array will be ['three', 'four']
Enter fullscreen mode Exit fullscreen mode

In the above example, grep is used to filter out the elements of the array that are of the String class. The result is a new array that contains only the string elements from the original array.

Okay maybe you knew those 3 already but that is just a tip of the iceberg. Keep your fingers crossed for the next set coming soon.

Top comments (5)

Collapse
 
ezekiel_77 profile image
Ezekiel

Truth here

Everything in ruby has blown my mind....

from pp to

def add(a,b) 
  c = a + b
  c  # no return statement
end

add 5 ,6 # 😲
Enter fullscreen mode Exit fullscreen mode

Nice blog btw 💫

Collapse
 
repiki profile image
Repiki • Edited

Interesting thing is you don't even need to add the last line with c. Just c = a+b or simply a+b (if you don't want to declare an extra variable and save some memory) in the method and you get your result back nicely.

Collapse
 
starswan profile image
Stephen Dicks

[].all?(&:even?) always returns true which can be a gotcha

Some comments may only be visible to logged-in visitors. Sign in to view all comments.