Posted: 12/12/2009 1:29:00 PM EDT
Who can tell me what the following two lines of code do:
FILE_DIR = "~/toburn/" 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.) |
|
Quoted: That code tells the Lotto machine that whatever number I pick is the Jackpot Winner? 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. I've been polishing it for months now. |
|
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? |
|
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. |