lsof
On linux, you could use lsof
:
puts `lsof -a -p #{Process.pid}`
It gives you the files held open by the current process.
The list is far from being complete, though : Once this line executes, many files have been read, parsed, executed and closed.
When lsof
scans for open files, they don't appear in the list anymore.
strace
strace
(also on Linux) is a great tool for what you want to achieve (see this thread). It will give a lot (possibly way too much) information :
strace -e trace=open -o opened_files.txt ruby hello_world.rb
#=> Hello world
opened_files.txt
now begins with :
open("/home/ricou/.rvm/rubies/ruby-2.3.1/lib/tls/x86_64/libruby.so.2.3", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/home/ricou/.rvm/rubies/ruby-2.3.1/lib/tls/libruby.so.2.3", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/home/ricou/.rvm/rubies/ruby-2.3.1/lib/x86_64/libruby.so.2.3", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/home/ricou/.rvm/rubies/ruby-2.3.1/lib/libruby.so.2.3", O_RDONLY|O_CLOEXEC) = 3
open("/home/ricou/.rvm/rubies/ruby-2.3.1/lib/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
open("/home/ricou/.rvm/rubies/ruby-2.3.1/lib/libpthread.so.0", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libpthread.so.0", O_RDONLY|O_CLOEXEC) = 3
open("/home/ricou/.rvm/rubies/ruby-2.3.1/lib/libgmp.so.10", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
....
It has 571 lines for a "Hello World" script and 112302 (!) for a "rails runner hello_world.rb" script.
The output file has many "ENOENT (No such file or directory)" lines, so you might want to parse and filter it.
For macOS and other *nix, there should be dtrace
.