Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does this superb quine work?

Tags:

ruby

quine

Another SO question mentions a fantastic quine by Yusuke Endoh. A quine is a computer program which produces a copy of its own source code as its only output; this one does it with a twist. A must-see.

But I can't figure out how it works. Any pointers?

like image 229
steenslag Avatar asked Dec 21 '10 21:12

steenslag


1 Answers

First, note that the v= line and the ASCII globe are the only bits of the code that change on each generation. This gives us a bit of a hint as to what's really going on here.

The basic principle is that the globe model was encoded with Zlib::Deflate in the first half of the code, and the second half expands it with Zlib::Inflate, reads it, and draws the ASCII globe within the new code, with the new v value. There's nothing particularly special about the 45° shifts; the code will draw the globe at whatever rotation you like. (In fact, it takes a command line argument specifying the number of degrees, if you like.)

For more implementation details, I'd recommend expanding the source code and tracing through it, with that basic understanding of how the quine is designed :)

like image 71
Matchu Avatar answered Sep 24 '22 11:09

Matchu