Basic Array Methods: A Practical Guide

Arrays are one of the most versatile and commonly used data structures in Ruby. They allow you to store and manipulate collections of elements efficiently. Ruby provides a rich set of methods for working with arrays, making it easy to add, remove, sort, transform, and filter data. In this blog post, we’ll explore some essential Ruby array methods, complete with examples and practical applications.


1. push

The push method adds elements to the end of an array.

1
2
3
arr = [1, 2, 3]
arr.push(4)
# => [1, 2, 3, 4]

Use push to grow an array dynamically, such as adding user inputs to a list.


2. delete

The delete method removes all occurrences of a specified element from an array.

1
2
3
arr = [1, 2, 3, 2]
arr.delete(2)
# => [1, 3]

This is useful when you need to clean up specific values from an array.


3. insert

The insert method allows you to add an element at a specific index.

1
2
3
arr = [1, 2, 4]
arr.insert(2, 3)
# => [1, 2, 3, 4]

This is handy when you need to place elements at specific positions in a list.


4. pop

The pop method removes and returns the last element of an array.

1
2
3
4
arr = [1, 2, 3]
arr.pop
# => 3
# arr is now [1, 2]

pop is useful when implementing stack-like behavior, where elements are removed in a last-in, first-out (LIFO) manner.


5. clear

The clear method removes all elements from an array.

1
2
3
arr = [1, 2, 3]
arr.clear
# => []

Use this method when you need to reset or empty an array completely.


6. concat

The concat method merges two arrays, appending the elements of the second array to the first.

1
2
3
4
arr1 = [1, 2]
arr2 = [3, 4]
arr1.concat(arr2)
# => [1, 2, 3, 4]

concat is useful when combining data from multiple sources into a single array.


7. map

The map method creates a new array by transforming each element of the original array according to the block provided.

1
2
3
arr = [1, 2, 3]
new_arr = arr.map { |n| n * 2 }
# => [2, 4, 6]

This is useful for applying transformations or calculations to all elements in an array.


8. select

The select method filters elements based on the block’s condition and returns a new array with the elements that satisfy the condition.

1
2
3
arr = [1, 2, 3, 4, 5]
even_numbers = arr.select { |n| n.even? }
# => [2, 4]

Use select when you need to filter data based on specific criteria.


9. reverse

The reverse method returns a new array with the elements in reverse order.

1
2
3
arr = [1, 2, 3]
arr.reverse
# => [3, 2, 1]

This is useful for tasks that require reversing the order of elements, like when displaying the most recent entries first.


10. flatten

The flatten method converts a multi-dimensional array into a one-dimensional array by removing nested arrays.

1
2
3
arr = [1, [2, 3], [4, [5]]]
arr.flatten
# => [1, 2, 3, 4, 5]

Use flatten when you need to normalize nested data into a flat structure.


11. compact

The compact method removes nil values from an array.

1
2
3
arr = [1, nil, 2, nil, 3]
arr.compact
# => [1, 2, 3]

This method is helpful when you want to clean up an array by removing nil values.


12. sort

The sort method returns a new array with elements sorted in ascending order. By default, sort works with numbers and strings.

1
2
3
arr = [3, 1, 2]
arr.sort
# => [1, 2, 3]

For more complex sorting, you can provide a block to customize the sorting logic.


13. join

The join method converts an array into a string, with each element separated by a specified delimiter.

1
2
3
arr = [1, 2, 3]
arr.join('-')
# => "1-2-3"

Use join when you need to create strings from array elements, such as generating CSV lines or URLs.


Ruby’s array methods provide powerful ways to manipulate and transform data collections efficiently. Whether you need to add or remove elements, filter and transform data, or flatten nested structures, these basic array methods make it easy to handle arrays in a wide range of applications.