Q:

Ruby program to fill an array with a specific element

belongs to collection: Ruby Arrays Programs

0

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.

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 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.

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

total answers (1)

Ruby Arrays Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Ruby program to find the largest element from the ... >>
<< Ruby program to fetch elements from an array based...