Ruby program to fill an array with a specific element

Ruby Example: Write a program to fill an array with a specific element.
Submitted by Nidhi, on January 17, 2022

Problem Solution:

In this program, we will create an array of cities. Then we will read a string from the user and fill the array with the input string using the fill() method.

Program/Source Code:

The source code to fill an array with a specific element is given below. The given program is compiled and executed successfully.

# Ruby program to fill an array 
# with specific element

city = ["AGRA", "DELHI", "MUMBAI", "GWALIOR"];

print "Enter string: ";
str = gets.chomp;  

city.fill(str);

print "Cities are: \n",city,"\n";

Output:

Enter string: hello
Cities are: 
["hello", "hello", "hello", "hello"]

Explanation:

In the above program, we created an array of city names. Here, we read a string from the user. Then we filled the array city with input string and printed the updated array.

Ruby Arrays Programs »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.