#!/usr/bin/env ruby abort("Usage: quineit.rb INFILE [OUTFILE]") if ARGV.length < 1 or ARGV.length > 2 input = ARGV[0] s = String.new t = String.new bang = String.new File.open(input) do |file| while line = file.gets next if line.nil? bang = line + "\n" and next if line =~ /#!/ #Can't put stuff before #!/usr/... s << line.inspect[1...-1] #Escape (but don't include the quotes t << line end end len = bang.inspect[1...-1].length f = (12 + len).to_s e = (13 + len).to_s out = ARGV[1] ? File.open(ARGV[1], "w") : STDOUT #Writing to file or stdout? out.print bang out.puts "def quine" out.puts "\ts = \"" + bang.inspect[1...-1] + "def quine\\n\\ts = \\n\\ts[0..#{f}] + s.inspect + s[#{e}..-1]\\nend\\n#{s}\"" out.puts "\ts[0..#{f}] + s.inspect + s[#{e}..-1]" out.puts "end" out.puts t out.close unless out == STDOUT