Find latest file ending with a number with Ruby

    Dir.entries(shared_path + '/database_dumps').drop(2).sort_by |s|
      s.scan(/\d+/).map{|s| s.to_i}.last

Broken down in english

Using the Dir class to use the method “entries” to look in the path, drop the first two entries “.” and “..”, sort by the results of the scan of \d+ (digits converted to an integer “to_i”). After all that, return the last value which should be the highest number.

comments powered by Disqus