To add or remove lements to/from array in Ruby we have different methhods. Array.push method to add elements into Array Example # creating an empty array arr1 = Array.new() # adding String element into arra1 arr1.push "another string"; puts arr1; puts "\n Addding New Element" # adding Integer element into arra1 arr1 .push 2; puts arr1; Output Array.pop method to remove lement from array pop() will remove one by one element from Array. Example # creating an empty array arr1 = Array.new() # adding String element into arra1 arr1.push "another string"; puts arr1; puts "\n adding New Element" # adding Integer element into arra1 arr1 .push 2; puts arr1; arr1.pop(); puts "\n remove last Element" puts arr1; arr1.pop(); puts "\n remove last Element" puts arr1; Output How to delete specifi index Element from Array in Ruby? Ruby Array has method delete_at() to delete element from specified index. Example # creating an empty array arr1 = Array.new() # adding String element into arra1 arr1.push "another string"; puts arr1; puts "\n adding New Element" # adding Integer element into arra1 arr1 .push 2; puts arr1; arr1.delete_at(1); puts "\n delete element at index 1" puts arr1; Output
another string
Addding New Element
another string
2
another string
adding New Element
another string
2
remove last Element
another string
remove last Element
another string
adding New Element
another string
2
delete element at index 1
another string
Ruby program to print an array
how to create an array with Array.[](*args) in Ruby ?
Ruby program to check whether the given number is palindrome
What are some of your favorite gems? What are their alternatives?
How to add/remove elements to Array in Ruby?
Ruby program to add two integer numbers
What are #method_missing and #send? Why are they useful?
How to Sort Array in Ruby?
How to get index of array element in Ruby
How to Get Input with Gets in Ruby
Ruby program to Calculate the factorial of given number
Ruby program to reverse a string
Ruby program to check whether the given number is prime or not
What's the difference between a lambda, a block and a proc?
Creating Array with Array.new(size, obj) in Ruby
What are the various Ruby runtimes, and how are they different?
Ruby program to check whether the given number is Armstrong
Ruby program to generate random numbers
Ruby program to print Fibonacci series
How to create two dimensional array in ruby?
How to shuffle an array in Ruby?
Program to Print Triangle of Numbers in Ruby
How to Replace array elements in Ruby?