How can I get the path for the last created file in a directory using Ruby?
If you want to get the path of a file relative to another, you can use the expand_path method with either the constant __FILE__ or the method __dir__ . Read this card for more information about __FILE__ and __dir__ .
permalink #__dir__ ⇒ StringReturns the canonicalized absolute path of the directory of the file from which this method is called. It means symlinks in the path is resolved. If __FILE__ is nil , it returns nil . The return value equals to File.
The value of __FILE__ is a relative path that is created and stored (but never updated) when your file is loaded. This means that if you have any calls to Dir.
I think this is fairly brief:
Dir.glob(File.join(path, '*.*')).max { |a,b| File.ctime(a) <=> File.ctime(b) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With