What is File.ctime in Ruby?

Overview

The ctime() method of a File instance in Ruby is used to return the change time for a particular file. Note that this time is when the file’s directory was changed, not the file itself.

Syntax

File.ctime(file_name)

Parameters

file_name is the name of the file.

Return value

The value returned is the directory change time for the file.

Code example

main.rb
test.rb
test.txt
# get file
fn1 = "test.txt"
fn2 = "test.rb"
# get change time
puts File.ctime(fn1)
puts File.ctime(fn2)

Explanation

  • Files test.rb and test.txt were created. We also have the main.rb file containing our application code.
  • Line 2 and 3: we got the file names.
  • Line 6 and 7: the change times of the files were obtained, and the values were printed to the console.

Free Resources