Sitemap
Win Feigle

Win Feigle

Basic Ruby enumerable and array methods with examples:

4 min readFeb 3, 2023

--

First things first. What is an array method, and what is the enumerable module? (Because it confused me at first)

An enumerable is a Ruby model that gives us the ability to traverse, search, and sort using its methods on collection classes. The enumerable module defines methods for working with collections of data: iterating through, filtering, and transforming elements.

Ruby has a multitude of built-in array methods, and some are more commonly used than others.

Enumerable methods can be used on arrays and other objects that include the enumerable module, while Array methods are specific to arrays and only arrays.

Array Methods

.concat combines elements from multiple arrays into one.

arr_1 = [1,2,3]
arr_2 = ["a","b","c"]

puts arr_1.concat([arr2])
#=> [1,2,3,"a","b","c"]

.delete and .delete_at: Both remove an element from an array.

arr = ["win", "lose", "draw"]

arr.delete("draw")
#=> ["win", "lose"]
arr = ["win", "lose", "draw"]

arr.delete_at(1)
#=> ["win", "draw"]

.drop: Returns an array after n elements have been removed.

arr = ["a","b","c","d","e","f"]

arr.drop(2)
#=> ["c", "d", "e", "f"]

.each: Used for iterating over the elements in an array.

names = ["Samantha","Wendy","Zeuss"]

names.each {|name| puts "Hello, #{name}."}

#=> Hello, Samantha.
#=> Hello, Wendy.
#=> Hello, Zeuss.

.map: Used for transforming each element in an array and returning the new, modified array with the results.

nums = [1,2,3]

new_nums = nums.map { |num| num*num }

puts nums
#=> [1,2,3]

puts new_nums
#=> [1,4,9]

.select: Used for filtering elements from an array based on a condition, you have to return something that evaluates to either true or false.

nums = [1,2,3,4]

even_nums = nums.select { |num| num.even? }

puts even_nums
#=> [2,4]

.find: Used for finding the first element in an array that satisfies a given condition.

nums = [1,2,3,4]

nums.find { |num| num.even? }

#=> 2

.sort: Used for sorting elements in an array.

nums = [78, 8, 22, 42]

nums.sort
#=> [8, 22, 42, 78]

.reverse: Used for reversing the order of elements in an array. It’s important to note this doesn't also sort the array.

nums = [8,78,22,42]

nums.reverse
#=> [42, 22, 78, 8]

nums.sort.reverse
#=> [78, 42, 22, 8]

.size or .length: Used for determining the number of elements in an array. Returns an integer, and 0 if empty.

Get Win Feigle’s stories in your inbox

Join Medium for free to get updates from this writer.

.first and .last: Used for accessing the first or last element in an array.

.empty?: Used for checking if an array is empty. Returns true or false.

.include?: Used for checking if an array contains a specific element. Returns true or false.

nums = [1,2,3,4]

nums.include?(3)
#=> true

Enumerable Methods

There are a few enumerable methods that work in the same way as array methods, some of these include: .each, .map, .select, .find, .sort, among others.

.min and .max: Used for finding the minimum or maximum element in a collection.

array = [3, 2, 5, 1, 4]
min_value = array.min
max_value = array.max

puts "Min: #{min_value} Max: #{max_value}"
#=> Min: 1 Max: 5

.flatten: Used to flatten nested arrays and return a single-dimensional array. The optional level argument can be used to control the level of flattening.

array = [[1, 2, 3], [4, 5, 6]]
flattened_array = array.flatten

puts flattened_array
# Output: [1, 2, 3, 4, 5, 6]

.flatten example with level argument

array = [[1, 2, [3, 4]], [5, 6]]
flattened_array = array.flatten(1)

puts flattened_array
#=> Output: [1, 2, [3, 4], 5, 6]

.with_index: Used in conjunction with the .each method to traverse an array, and it provides the index of each item in the array as it's being traversed.

array = ["dog", "cat", "bird"]
array.each.with_index do |item, index|
puts "Index: #{index} Item: #{item}"
end

#=> Index: 0 Item: dog
#=> Index: 1 Item: cat
#=> Index: 2 Item: bird

.all?: Used for checking if all elements in a collection satisfy a given condition.

array = [1, 2, 3, 4, 5]
all_even = array.all? do |num|
num.even?
end

puts all_even
#=> false

.any?: Used for checking if any elements in a collection satisfy a given condition.

array = [1, 2, 3, 4, 5]
any_even = array.any? do |num|
num.even?
end

puts any_even
#=> true

.reduce: Used for combining all elements in a collection into a single value.

array = [1, 2, 3, 4, 5]
sum = array.reduce(0) do |total, num|
total + num
end

puts sum
#=> 15

.group_by: Used for grouping elements in a collection based on a specific attribute.

array = [1, 2, 3, 4, 5]
grouped_array = array.group_by do |num|
num.even?
end

puts grouped_array
# Output: {false=>[1, 3, 5], true=>[2, 4]}

In conclusion… we’ve barely scratched the surface of using methods on arrays and collections. To learn more, here are some additional resources:

--

--