From: Daniel Berger Date: 2005-08-19T01:39:09+09:00 Subject: Re: Idiomatic ruby version of this code? Brock Weaver wrote: > 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 Dir["*.mp3"].each{ |file| new_name = File.basename(file, ".mp3") + ".temp" File.rename(file, new_name) } Regards, Dan