From: Urban Hafner Date: 2001-03-10T06:13:37+09:00 Subject: [ruby-talk:12360] Re: File with length 0 Sorry for my late answer, but I've been busy. Tammo Freese wrote: [...] >2) If you run your program below, there are cases where the changed program > can be run, too. As you run it with "ruby", it will actually be executed > and lead to additional files! I know and that was my intention! But nevertheless thank you for your suggestions. As you might see in the code I've used your suggestion with the -c option, but solved the time problem with sleep(0.15) because otherwise I would get to long names. I'd like to get some suggestions on improvements of my code. Urban -- "coordinator.rb" workingdir = Dir.getwd print "Number of generations: " generations = gets generations = generations.to_i 1.upto(generations) do files = Dir.entries("evolution") files.delete("."); files.delete("..") Dir.chdir("evolution") files.each {|child| if system("ruby","-c","#{child}") == false File.delete("#{child}") files.delete("#{child}") else sleep(0.15) if system("ruby","#{child}") == false File.delete("#{child}") files.delete("#{child}") end end } Dir.chdir("#{workingdir}") end -- "evolution.rb" get = File.new($0,"r") child = "#{Time.now.to_f}" put = File.new(child,"w") lines = get.readlines 1.upto(Kernel.rand(0.5*lines.length)) do changeline = Kernel.rand(lines.length-1) string = lines[changeline] changechar = Kernel.rand(string.length-1) if Kernel.rand(128) == 128 string[changechar] = "" else chars = Kernel.rand(127).chr 2.upto(Kernel.rand(5)) do chars = chars + Kernel.rand(127).chr end string[changechar] = chars end lines[changeline] = string end lines.each {|item| put.puts(item)} put.close; get.close -- "install.rb" Dir.mkdir("evolution") get = File.open("evolution.rb","r") Dir.chdir("evolution") put = File.open("evolution.rb","a") get.each {|line| put.puts(line)} get.close put.close