To find the root directory of a directory in Ruby, we can use the Filesystem
module and the dirname
method of the File
class to get the parent directory path repeatedly until we reach the root directory. Here's an example code block:
main.rb111 chars6 lines
In this code, we start with a directory path (/path/to/directory
in this example) and loop through the parent directories using File.dirname
until we reach the root directory (which has the path /
). Finally, we print out the root directory path.
gistlibby LogSnag