Q:

Ruby program to get the list the name of files in a specified directory

belongs to collection: Ruby Directories Programs

0

In this program, we will get the list of filenames in the specified directory and print the result.

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

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

total answers (1)

Ruby program to get the current working directory ... >>
<< Ruby program to remove a specified directory...