Add array elements in Ruby Last Updated : 20 Jul, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we will learn how to add elements to an array in Ruby.Method #1: Using Index Ruby # Ruby program to add elements # in array # creating string using [] str = ["GFG", "G4G", "Sudo", "Geeks"] str[4] = "new_ele"; print str # in we skip the elements str[6] = "test"; Output: ["GFG", "G4G", "Sudo", "Geeks", "new_ele"] ["GFG", "G4G", "Sudo", "Geeks", "new_ele", nil, "test"] Method #2: Insert at next available index (using push() method) - Ruby # Ruby program to add elements # in array # creating string using [] str = ["GFG", "G4G", "Sudo", "Geeks"] str.push("Geeksforgeeks") print str Output: ["GFG", "G4G", "Sudo", "Geeks", "Geeksforgeeks"] Method #3: Using << syntax instead of the push method - Ruby # Ruby program to add elements # in array # creating string using [] str = ["GFG", "G4G", "Sudo", "Geeks"] str << "Geeksforgeeks" print str Output: ["GFG", "G4G", "Sudo", "Geeks", "Geeksforgeeks"] Method #4: Add element at the beginning - Ruby # Ruby program to add elements # in array # creating string using [] str = ["GFG", "G4G", "Sudo", "Geeks"] str.unshift("ele_1") print str Output: ["ele_1", "GFG", "G4G", "Sudo", "Geeks"] Comment More infoAdvertise with us Next Article Ruby | Array to_a() function S Shivam_k Follow Improve Article Tags : Ruby Ruby Array Similar Reads How to access array Elements in Ruby In this article, we will learn how to access array elements in Ruby. In Ruby, there are several ways to retrieve the elements from the array. Ruby arrays provide a lot of different methods to access the array element. But the most used way is to use the index of an array. Using Index - ruby # Ruby p 2 min read Ruby | Array append() function Array#append() is an Array class method which add elements at the end of the array. Syntax: Array.append() Parameter: - Arrays for adding elements. - elements to add Return: Array after adding the elements at the end. Example #1 : Ruby # Ruby code for append() method # adding elements at the end # d 2 min read Ruby | Array to_a() function Array#to_a() : to_a() is a Array class method which returns self array. Syntax: Array.to_a() Parameter: Array Return: self array representation Example #1 : Ruby # Ruby code for to_a() method # declaring array a = [18, 22, 33, nil, 5, 6] # declaring array b = [1, 4, 1, 1, 88, 9] # declaring array c 1 min read Ruby | Array collect() operation Array#collect() : collect() is an Array class method which invokes the argument block once for each element of the array. A new array is returned which has the value returned by the block. Syntax: Array.collect() Parameter: Arrays in which we want elements to be invoked Return: array with all the en 1 min read Ruby | Array collect!() operation Array#collect!() : collect!() is an Array class method which invokes the argument block once for each element of the array, replacing the element with the value returned by the block Syntax: Array.collect!() Parameter: Arrays in which we want elements to be invoked Return: array with all the envoked 1 min read Ruby | Array count() operation Array#count() : count() is a Array class method which returns the number of elements in the array. It can also find the total number of a particular element in the array. Syntax: Array.count() Parameter: obj - specific element to found Return: removes all the nil values from the array. Code #1 : Exa 2 min read Like