Q:

Ruby program to append an element into an array

0

In this program, we will create an array with few elements. Then we will append an element into the array using the append() method.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to append an element into the array is given below. The given program is compiled and executed successfully.

# Ruby program to append an 
# element into array

arr = [101,102,103,104,105];

print "Array elements before append(): \n",arr,"\n\n";

arr.append(106);

print "Array elements after append(): \n",arr,"\n";

Output:

Array elements before append(): 
[101, 102, 103, 104, 105]

Array elements after append(): 
[101, 102, 103, 104, 105, 106]

Explanation:

In the above program, we created an array of integers with few elements. Then we added a new element into the array using the append() method and printed the updated array.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now