Showing off ruby to a coworker, and I want to emphasize how succinct yet
clear it can be. So I want to rename all .mp3 files to .temp, or vice versa
in the current directory -- in the shortest LOC *that you would actually write*;
not the "here's a one-liner nobody in their right mind would every write if
they had to maintain it".
Here's my first solution:
Dir.new("./").each do |file|
if file.match(/mp3$/)
File.rename(file, file.gsub(/mp3$/, 'temp'))
elsif file.match(/temp$/)
File.rename(file, file.gsub(/temp$/, 'mp3'))
end
end
Note you can NOT do the following, as it renames mp3 to temp,
then immediately renames that same file back to mp3...
Dir.new("./").each do |file|
File.rename(file, file.gsub(/mp3$/, 'temp')) if file.match(/mp3$/)
File.rename(file, file.gsub(/temp$/, 'mp3')) if file.match(/temp$/)
end
Any ideas?
--
Brock Weaver
[OBC]Technique