dirname()
is a File class method in Ruby that returns all components of a specified file name, except the last one. For example, /home/okwudili
returns /okwudili
when invoked.
File.dirname(file_name)
file_name
: This is the file name whose components we want to return.This method returns a string containing all components of the file name file_name
, except the last one.
# Get the directory names of the filesputs File.dirname("/home/okwudili/code/main.rb")puts File.dirname("/main.rb")puts File.dirname("/home/okwudili/code/ruby/main.rb")
dirname()
method. Then, we print the values to the console.