How to remove the last element of an array in Ruby
In this post we will give you information about How to remove the last element of an array in Ruby. Hear we will give you detail about How to remove the last element of an array in RubyAnd how to use it also give you demo for it if it is necessary.
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.popputs prices
Output:
[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 prices
Output:
[10, 20, 30]
Note: The pop() and delete_at() methods modifies the original array.
Hope this code and post will helped you for implement How to remove the last element of an array in Ruby. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs