What is File.absolute_path() in Ruby?

Overview

The absolute_path() method of the File class in Ruby converts the path-name of a file to an absolute path-name.

Syntax

File.absolute_path(path_name)

Parameters

path_name: This is the path-name of the file. It is the relative path of the file whose starting point or absolute file path we want to get.

Return value

The absolute file name of path_name is returned.

Code example

main.rb
test2.txt
test1.txt
Welcome to Edpresso!

Explanation

  • We create two files: test1.txt and test2.txt.
  • Lines 2 to 3: We get the path-names of the files whose absolute file paths we need.
  • Lines 6 to 7: The absolute_path() is invoked to get the absolute paths of the files. Then, we print the results to the console.

Free Resources