Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby
communitycreator

What is array.push() in Ruby?

okwudili onyejiaku

The .push method of an array in Ruby adds an element to the end of an array. When it is done, it returns the array itself with the pushed elements.

Syntax

array.push([element])

Parameter

  • [element]: This could be one or more elements to be added at the end of the given array.

Return value

The push() method returns the array of the pushed element.

Example

In the example below, we will create some arrays. Then later, we use the push() method to add other elements to these arrays and print the arrays to the console.

# Initializing some arrays of elements
Array1 = [1, 2, 3]
Array2 = ["a", "b", "c"]
Array3 = ["Javascript", "Python", "C++"]

# Using the push() method
A = Array1.push(4,5)
B = Array2.push("d", "e")
C = Array3.push("Ruby", "Java")

# Print array of pushed elements
puts "#{A}"
puts "#{B}"
puts "#{C}"

RELATED TAGS

ruby
communitycreator
RELATED COURSES

View all Courses

Keep Exploring