From: ara.t.howard@... Date: 2006-12-15T00:07:30+09:00 Subject: Re: Swap contents in two files On Thu, 14 Dec 2006, Christopher Latif wrote: > Robert Klemme wrote: >> 2006/12/14, Christopher Latif : >>> I have two files, I want to swap the contents of the files in my >>> program, suggestions? >> >> Renaming with a third temp file name is the most efficient if you know >> that no file handles are open on these files. >> >> robert > > How the code for that program going to look like? harp:~ > cat a.rb require 'tempfile' require 'fileutils' def swap a, b fu, f = FileUtils, File tmpdir = f.dirname b t = Tempfile.new f.basename(b), f.dirname(b) t.close c = t.path fu.mv b, c fu.mv a, b fu.mv c, a end a, b, ignored = ARGV swap a, b harp:~ > cat a a harp:~ > cat b b harp:~ > ruby a.rb a b harp:~ > cat a b harp:~ > cat b a -a -- if you find yourself slandering anybody, first imagine that your mouth is filled with excrement. it will break you of the habit quickly enough. - the dalai lama