Home »
Ruby Tutorial »
Ruby Programs
Ruby program to get the list the name of files in a specified directory
Last Updated : December 15, 2025
Problem Solution
In this program, we will get the list of filenames in the specified directory and print the result.
Program/Source Code
The source code to list the name of files in the specified directory is given below. The given program is compiled and executed on Windows 10 Operating System successfully.
# Ruby program to list the name of files
# in a specified directory
filenames = Dir::entries("MyDir");
puts "File names: ",filenames;
Output
File names:
.
..
SampleFile1.txt
SampleFile2.txt
SampleFile3.txt
SampleFile4.txt
Explanation
In the above program, we get the list of filenames in the MyDir directory using the Dir::entries() method and print the result.
Ruby Directories Programs »
Advertisement
Advertisement