Path#dirname is correct to get the path of the parent directory.
However, Path[PROGRAM_NAME].expand only works correctly when the program is executed via a relative path. PROGRAM_NAME is whatever the user typed into their shell to run the program. If itās a path relative to the current directory, Path#expand works fine (and also if itās an absolute path, of course).
If however itās only a name thatās resolved in $PATH, this mechanism would be broken.
Returns an absolute path to the executable file of the currently running program. This is in opposition to PROGRAM_NAME which may be a relative or absolute path, just the executable file name or a symlink.
The executable path will be canonicalized (all symlinks and relative paths will be expanded).
Returns nil if the file canāt be found.
Although, i really donāt know why THE fILE CAN'T BE FOUND in this case, maybe core member can give some detailed explain.
The workaround is: puts Process.executable_path.as(String).dirname
Path#dirname is just a convention to colloquially reference a method. Itās not intended as source code.
A simple reason for this would be that the executable file which spawned the current process has since been removed.
For example demonstrated by the following program:
if path = Process.executable_path
File.delete(path)
end
Process.executable_path # => nil
This is just exposing a feature of the operating system and there might be all kinds of other reasons why the OS cannot report the path name or why itās not available to the current process.