ADVERTISEMENT
ADVERTISEMENT

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 »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.