Warning

 

Close
Confirm Action

Are you sure you wish to do this?

Cancel Confirm
AR15.COM
12/12/2009 1:29:00 PM EDT
Who can tell me what the following two lines of code do:

FILE_DIR = "~/toburn/"

Dir.entries(FILE_DIR).reject{|f| f.match(/mp3$/i).nil?}.sort{|x,y| File.new(FILE_DIR + x).mtime <=> File.new(FILE_DIR + y).mtime }.each_with_index{|file, i| File.rename(FILE_DIR + file, FILE_DIR + (i+1).to_s.rjust(3,'0') + '_' + file)}


This is going to be shown to students in a class I'm teaching as an example of poorly written code.

(Edited to fix grammar.)
12/12/2009 1:34:27 PM EDT
[#1]





Quoted:



Who can tell me what the following two lines of code do:
FILE_DIR = "~/toburn/"Dir.entries(FILE_DIR).reject{|f| f.match(/mp3$/i).nil?}.sort{|x,y| File.new(FILE_DIR + x).mtime <=> File.new(FILE_DIR + y).mtime }.each_with_index{|file, i| File.rename(FILE_DIR + file, FILE_DIR + (i+1).to_s.rjust(3,'0') + '_' + file)}






This is going to be show to students in a class I'm teaching as the bad way of writing code.





That code tells the Lotto machine that whatever number I pick is the Jackpot Winner?    




I've been polishing it for months now.





 
12/12/2009 2:05:49 PM EDT
[#2]
it tells me i need to keep my day job and NOT become a programmer


brian
12/12/2009 4:22:11 PM EDT
[#3]
my guess is that is it supposed to organize the .mp3 files in a directory by creating/rewriting sub directories and file names.


I dunno though, im not a coder. Thats just what i would guess
12/12/2009 5:37:18 PM EDT
[#4]
Quoted:
Who can tell me what the following two lines of code do:

FILE_DIR = "~/toburn/"Dir.entries(FILE_DIR).reject{|f| f.match(/mp3$/i).nil?}.sort{|x,y| File.new(FILE_DIR + x).mtime <=> File.new(FILE_DIR + y).mtime }.each_with_index{|file, i| File.rename(FILE_DIR + file, FILE_DIR + (i+1).to_s.rjust(3,'0') + '_' + file)}


This is going to be shown to students in a class I'm teaching as an example of poorly written code.

(Edited to fix grammar.)


Great example –– that's absolutely horrid code.  It looks like Ruby?
12/13/2009 11:45:21 AM EDT
[#5]
Quoted:
Quoted:
Who can tell me what the following two lines of code do:

FILE_DIR = "~/toburn/"Dir.entries(FILE_DIR).reject{|f| f.match(/mp3$/i).nil?}.sort{|x,y| File.new(FILE_DIR + x).mtime <=> File.new(FILE_DIR + y).mtime }.each_with_index{|file, i| File.rename(FILE_DIR + file, FILE_DIR + (i+1).to_s.rjust(3,'0') + '_' + file)}


This is going to be shown to students in a class I'm teaching as an example of poorly written code.

(Edited to fix grammar.)


Great example –– that's absolutely horrid code.  It looks like Ruby?


Yeah, it is a Ruby script that takes all the MP3 files in a directory, sorts them by date and then renames them so that each file name is prefixed with it's order in the list.

The non-ugly version is about 15 lines.