Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active March 7, 2023 10:06
Show Gist options
  • Save JoshCheek/c634f776fa57addee032cf174c2de629 to your computer and use it in GitHub Desktop.
Save JoshCheek/c634f776fa57addee032cf174c2de629 to your computer and use it in GitHub Desktop.
A program that is both a palindrome and a quine, made with hearts for Joe

A program that is a palindrome (if you reverse its source code, you get the same program) and is a quine (if you run it, it prints out its own source code). Verify like this:

ruby palindrome_quine.rb | tee output
diff palindrome_quine.rb output # the output is the same as the source program
ruby -e 'puts $stdin.read.reverse' < palindrome_quine.rb > reversed
diff palindrome_quine.rb reversed # the reversed program is the same as the source program
printf a="%2$sprintf a=%1$p , a , $/ , a.reverse%2$s__END__%2$s__DNE__%2$sesrever.a , /$ , a , %3$p=a ftnirp%2$s" , a , $/ , a.reverse
__END__
__DNE__
esrever.a , /$ , a , "s$2%printf a=p$3% , a , $/ , a.reverses$2%__END__s$2%__DNE__s$2%esrever.a , /$ , a , p$1%=a ftnirps$2%"=a ftnirp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment