How to remove the last element of an array in Ruby
In this tutorial, we are going to learn about how to remove the last element of a given array in Ruby.
Consider, we have the following array:
prices = [10, 20, 30, 40]Now, we want to remove the last element 40 from the above array.
Removing the last element
To remove the last element of an array, we can use the built-in pop method in Ruby
Here is an example, that removes the last element 40 from the prices array.
prices = [10, 20, 30, 40]
prices.pop
puts pricesOutput:
[10, 20, 30]Similarly, we can also use the delete_at() method to remove the last element.
prices = [10, 20, 30, 40]
prices.delete_at(prices.length-1)
puts pricesOutput:
[10, 20, 30]Note: The pop() and delete_at() methods modifies the original array.


