Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the path for the last created file in a directory using Ruby?

Tags:

file

windows

ruby

How can I get the path for the last created file in a directory using Ruby?

like image 322
Junior Developer Avatar asked Jul 02 '10 13:07

Junior Developer


People also ask

How do I get the file path in 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__ .

What is __ Dir __ in Ruby?

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.

What does __ file __ mean in Ruby?

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.


1 Answers

I think this is fairly brief:

Dir.glob(File.join(path, '*.*')).max { |a,b| File.ctime(a) <=> File.ctime(b) }
like image 175
Mike Woodhouse Avatar answered Oct 16 '22 13:10

Mike Woodhouse