Skip to content

How to get the names of all the files in a folder in Ruby

When it comes to working with directories Ruby comes with a class for dealing with directories and paths. Ruby Dir Doc Page.

If you want a quick demo of how to get the names of files in a directory, look no further!

## Gets the current path of your Ruby script/console
Dir.pwd
## ["array.rb", "of", "files.rb", "and", "folders"]
Dir.children(Dir.pwd)

You can also provide an instance a directory(Dir instance or string of file/folder name)

Dir.new(Dir.pwd).each {|f| puts "Got #{f}"}
Got .
Got ..
Got app