in programming

Ruby Quine Example

I was recently challenged to write a quine in the language of my choice.
If you are unsure what a quine is you can find out from However basically in our case it is a piece of code that when executed creates a copy of itself. This copy can then be executed and ad infinitum.

There are some official rules about how a quine should be implemented:

  • A quine must not take any input, as this would allow the source code to be fed in from the keyboard.
  • Following on from the previous rule a quine, should not open it’s own file and display it’s contents.

Anyway here is my solution in Ruby…

 lambda{|x| puts x + x.inspect}.call "lambda{|x| puts x + x.inspect}.call"

Here is a brief description of what is happening. The lambda creates an anonymous function that takes one parameter and outputs it’s value

Officially I think passing a value into the lambda x attribute violates one of the rules – but non-the-less it is quite a clear example and may be useful to get an idea of how a quine works.

Here is a list of interesting links that might help, good luck:

Write a Comment