Tuesday, April 17, 2012

Brilliant 8-phase multi-quine in ruby

Wikipedia defines quine as:
A quine is a computer program which takes no input and produces a copy of its own source code as its only output.
In layman terms, it is a program that when executed outputs itself. Like a C program outputting its own source code. Kleene's Recursion Theorem proves the existence of quines in any Turing Complete Language. Its a good exercise to write one in your favorite language. Its fascinating to see quines.

Today morning my friend Umesh showed me this brilliant piece of ruby code that behaves as a Multi-quine. Link to original post by its author. Here's a listing of the same:



Save it as "test.rb" and run the following shell script:
while true;
do
    clear
    ruby test.rb | tee test2.rb
    mv test2.rb test.rb
    sleep 1
done

You will be able to see the picture changing after every 1 second and returning back to the original after 8 seconds. Note that when you run test.rb, it produces the output which is used for the next iteration. The shell script code lines 4 and 5 runs the code and uses the output of the code to replace the original code.

After being amazed by the beauty of this piece of code, I am not trying to figure out how exactly does it achieve this result. Someday I will post the same code with indentations and explanations in this blog.

No comments:

Post a Comment

I'd love to hear from you !