×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Ruby program to fill an array with a specific element

Last Updated : December 15, 2025

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!

Load comments ↻


Advertisement
Advertisement
Advertisement

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