Home »
Ruby »
Ruby Programs
Ruby program to get the list of filenames matching the specified wild card pattern
Ruby Example: Write a program to get the list of filenames matching the specified wild card pattern.
Submitted by Nidhi, on February 21, 2022
Problem Solution:
In this program, we will get the list of filenames matching the specified wild card pattern and print the result.
Program/Source Code:
The source code to get the list of filenames matching the specified wild card pattern is given below. The given program is compiled and executed on Windows 10 Operating System successfully.
# Ruby program to get the list of filenames
# matching the specified wild card pattern
filenames = Dir::entries("MyDir");
puts "All Filenames: ",filenames;
filenames = Dir::glob("MyDir/Sample*");
puts "\nFile names: ",filenames;
Output:
All Filenames:
.
..
Data.txt
SampleFile1.txt
SampleFile2.txt
SampleFile3.txt
SampleFile4.txt
File names:
MyDir/SampleFile1.txt
MyDir/SampleFile2.txt
MyDir/SampleFile3.txt
MyDir/SampleFile4.txt
Explanation:
In the above program, we get the list of filenames using the Dir::glob() method with a specified wild card pattern and print the result.
Ruby Directories Programs »