Ruby

Remove an Array Element in Ruby

Arrays are some of the most versatile and popular data structures in Ruby and other programming languages. Although the implementation may differ based on various programming languages, in Ruby, we can define an array as a collection of items of various data types, including other arrays.

Arrays in Ruby are ordered and can be accessed using indexes. An array index refers to an integer value that denotes the position of an element in an array. In Ruby, arrays are objects from the Array class, which contains a set of methods and useful data structures for working with arrays.

This post will discuss the fundamentals of working with arrays, including how to remove an element from an array in Ruby.

Ruby Create Array

In Ruby, we can create an array using a square bracket and add the elements of the array inside it. Each element of the array is separated by a comma, as shown in the syntax below:

array = [element1, element2, element3…elementN]

For example:

top_databases = ["MySQL", "PostgreSQL", "Oracle Database", "SQL Server", "MongoDB"]

In this case, we create an array called top_database. The array contains five elements of string type, each representing a different database.

We can also use the Array.new method to create a new array. An example is as shown below:

top_databases = Array.new(["MySQL", "PostgreSQL", "Oracle Database", "SQL Server", "MongoDB"])

Ruby Remove Element from Array

The reject method is the first method to remove an element from an array.

The reject method allows us to filter elements from an array or an enumerable object based on a specific condition. The method takes a block as an argument and the block should return true or false for each element. Elements for which the block returns true are removed from the original collection and a new collection containing the remaining ingredients is returned.

It is good to remember that the method will not alter the original array. Instead, it creates a new array to store the elements.

Take the example below:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
odd_numbers = numbers.reject { |n| n % 2 == 0 }

In the example above, we use the reject method on the numbers array. The block will take the elements n and returns true if the element is even; false if the element is odd. The reject method will then remove all the elements that are true and store the ‘false’ values in the new array.

Output:

=> [1, 3, 5, 7, 9]

Remove Element From Array

The subtraction method is the second method we can use to remove an element from an array. This allows us to subtract two arrays and store similar values.

An example is as shown below:

numbers_to_remove = [3, 4, 5]
original_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
new_array = original_array - numbers_to_remove

Resulting array:

=> [1, 2, 6, 7, 8, 9, 10]

In this example, the new_array contains all elements from the original_array except the ones that were present in the numbers_to_remove array.

We can use this method to remove multiple elements at once. This is a more efficient way than looping over the array and removing elements one by one.

It is worth noting that this method only removes elements if they exist in the original array and if they are the exact same object, not if they are equal.

Ruby Delete Element From Array – Method 3

Ruby also provides us with the delete method which allows us to remove a specified element from a given array

The function will delete the specified element and return it as the output. It is good to keep in mind that this is a destructive method, this means it will make modifications to the original array as demonstrated below:

arr = [1,2,3,4,5,6]
arr.delete(5)
=> 5

Output:

irb(main):040:0> puts arr
1
2
3
4
6
=> nil

Ruby Delete Element From Array – Method 4

We can also delete an element from an array using the element’s index position using the delete_at() method.

The method accepts the target index whose element we wish to delete as the parameter. The syntax is as shown below:

array.delete_at(index)

The index denotes the index position of the element we wish to delete.

Similarly, the method will return the value of the element we deleted.

An example is shown below:

original_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
original_array.delete_at(2)
=> 3

In this example, the delete_at function and pass the target index as 2. In this case, the function removes the element at the specified index, 3 and modifies the original array.

The function will also return an error if the specified index is out of range. To avoid modifying the original array, you can use other methods discussed above or square bracket notation.

original_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
new_array = original_array[0...2] + original_array[3..-1]

This works by removing the element at the target position and concatenating the resulting arrays into a single value.

Conclusion

In this, you came across four main methods you can use to remove an element from an input array using the Ruby programming language.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list