What is File.dirname() in Ruby?
Overview
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.
Syntax
File.dirname(file_name)
Parameters
file_name: This is the file name whose components we want to return.
Return value
This method returns a string containing all components of the file name file_name, except the last one.
Code example
# 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")
Explanation
- Lines 2 to 4: We get the directory names of some files using the
dirname()method. Then, we print the values to the console.