B
Brock Weaver
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 vers=
a
in the current directory -- in the shortest LOC *that you would actually wr=
ite*;
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|
=09if file.match(/mp3$/)
=09=09File.rename(file, file.gsub(/mp3$/, 'temp'))
=09elsif file.match(/temp$/)
=09=09File.rename(file, file.gsub(/temp$/, 'mp3'))
=09end
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|
=09File.rename(file, file.gsub(/mp3$/, 'temp')) if file.match(/mp3$/)
=09File.rename(file, file.gsub(/temp$/, 'mp3')) if file.match(/temp$/)
end
Any ideas?
--=20
Brock Weaver
[OBC]Technique
clear it can be. So I want to rename all .mp3 files to .temp, or vice vers=
a
in the current directory -- in the shortest LOC *that you would actually wr=
ite*;
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|
=09if file.match(/mp3$/)
=09=09File.rename(file, file.gsub(/mp3$/, 'temp'))
=09elsif file.match(/temp$/)
=09=09File.rename(file, file.gsub(/temp$/, 'mp3'))
=09end
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|
=09File.rename(file, file.gsub(/mp3$/, 'temp')) if file.match(/mp3$/)
=09File.rename(file, file.gsub(/temp$/, 'mp3')) if file.match(/temp$/)
end
Any ideas?
--=20
Brock Weaver
[OBC]Technique