#!/usr/bin/env ruby #Author: Eric Stratmann #Date: 2006-12-01 #Description: Takes a ruby file as input and outputs a modified file # with a new method quine which returns the source code # of the modified file abort("Usage: quineit.rb INFILE [OUTFILE]") if ARGV.length < 1 or ARGV.length > 2 input = ARGV[0] bang = String.new file = File.readlines(input) #Read file into an array bang = file[0] + "\n" if file[0] =~ /#!/ #We can't put anything before #!/usr/... escaped = file.map{|line| line.inspect[1...-1]}.flatten file.flatten! start = "def quine\n\ts = " #Beginning of string, so we can test length loc = start.length + bang.length - 1 #Everything up to "s = " in the string out = ARGV[1] ? File.open(ARGV[1], "w") : STDOUT #Writing to file or stdout? out.print bang out.print "#{start}\"#{bang.inspect[1...-1]}def quine\\n\\ts = \\n\\t" out.puts "s[0..#{loc}] + s.inspect + s[#{loc + 1}..-1]\\nend\\n\\n#{escaped}\"" out.puts "\ts[0..#{loc}] + s.inspect + s[#{loc + 1}..-1]" out.puts "end" out.puts out.puts file