Ruby | push() function Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The push() function in Ruby is used to push the given element at the end of the given array and returns the array itself with the pushed elements. Syntax: push(Elements) Parameters: Elements : These are the elements which are to be added at the end of the given array. Returns: the array of pushed element. Example 1: Ruby # Initializing some arrays of elements Array1 = [1, 2, 3, 4] Array2 = ["a", "b", "c"] Array3 = ["gfg", "Geeks", "GeeksforGeeks"] # Calling push() function A = Array1.push(5, 6, 7) B = Array2.push("d", "e", "f") C = Array3.push("Geek") # Printing the array of pushed element puts "#{A}" puts "#{B}" puts "#{C}" Output: [1, 2, 3, 4, 5, 6, 7] ["a", "b", "c", "d", "e", "f"] ["gfg", "Geeks", "GeeksforGeeks", "Geek"] Example 2: Ruby # Initializing some arrays of elements Array1 = [10, 20, 30, 40] Array2 = ["Z", "Y", "X"] Array3 = ["ab", "abc", "abcd"] # Initializing some elements # which are to be pushed p = 50, 60 q = "W", "V", "U" r = "abcde", "abcdef" # Calling push() function A = Array1.push(p) B = Array2.push(q) C = Array3.push(r) # Printing the array of pushed element puts "#{A}" puts "#{B}" puts "#{C}" Output: [10, 20, 30, 40, [50, 60]] ["Z", "Y", "X", ["W", "V", "U"]] ["ab", "abc", "abcd", ["abcde", "abcdef"]] Note: In the above example, it can be seen that if we initialize the parameters of the function in a separate variable then it gives output as an array inside the array shown above. Reference: https://devdocs.io/ruby~2.5/array#method-i-push Comment More infoAdvertise with us Next Article Ruby | Queue push() function K Kanchan_Ray Follow Improve Article Tags : Ruby Ruby-Methods Similar Reads Ruby | Array push() function Array#push() : push() is a Array class method which appends the given object(s) on to the end of this array. Syntax: Array.push() Parameter: Array Return: appends the given object(s) on to the end of this array. Example #1 : Ruby # Ruby code for push() method # declaring array a = [18, 22, 33, nil, 2 min read Ruby | Queue push() function The push() is an inbuilt function in Ruby inserts the element in the queue. Syntax: q_name.push(element) Parameters: The function takes the element to be inserted into the queue. Return Value: It inserts the element into the queue. Example 1: Ruby #Ruby program for push() function in Queue #Creat 1 min read Ruby | pop() function The pop() function in Ruby is used to pop or remove the last element of the given array and returns the removed elements. Syntax: pop(Elements) Parameters: Elements : This is the number of elements which are to be removed from the end of the given array. If this parameter is not used then it removes 1 min read Ruby | Hash key() function Hash#key() is a Hash class method which gives the key value corresponding to the value. If value doesn't exist then return nil. Syntax: Hash.key() Parameter: Hash values Return: key corresponding to the value nil - If value doesn't exist Example #1 : Ruby # Ruby code for Hash.key() method # declarin 2 min read Ruby | Hash to_s() function Hash#to_s() is a Hash class method which gives the string representation of hash. Syntax: Hash.to_s() Parameter: Hash values Return: string representation of hash Example #1 : Ruby # Ruby code for Hash.to_s() method # declaring Hash value a = {a:100, b:200} # declaring Hash value b = {a:100, c:300, 1 min read Ruby | Queue pop() function The pop() is an inbuilt function in Ruby returns the element in the front of the queue and removes it from the queue. Syntax: q_name.pop() Parameters: The function does not takes any element. Return Value: It returns the first element which is at the front of the queue and removes it from the queue. 1 min read Like