find the last modified date of a file in ruby

To find the last modified date of a file in Ruby, you can use the mtime method from the File class. Here's an example:

main.rb
file_path = "/path/to/your/file.txt"
last_modified = File.mtime(file_path)
puts "Last modified date: #{last_modified}"
119 chars
4 lines

In this example, we're passing the file path to the mtime method, which returns a Time object representing the last modified date of the file. We're then printing out that date using string interpolation.

Note that if the file doesn't exist or the user running the script doesn't have permission to access it, mtime will raise an exception. So you might want to add some error handling around the call to mtime if you're not sure whether the file will be accessible.

gistlibby LogSnag