Shortest Ruby Quine
Unfortunately RubyGarden doesn't exist anymore. Here are a couple of links to make up for it (the one Kevin posted is not the shortest one anymore by the way):
The first quines in Ruby
s="s=%c%s%c; printf s,34,s,34,10%c"; printf s,34,s,34,10
ruby quine slightly smaller than python quine
_="_=%p;puts _%%_";puts _%_
shortest nozero [sic!] ruby quine
puts <<2*2,2
puts <<2*2,2
2
Ruby: Print source code
A Quine is a program that prints out its own source code as its only functionality and yes you can do this without reading the source code file. The real challenge, and fun, is to write as short a Quine as possible.
The above would not be a Quine because it doesn't print its own source code out and it couldn't be because it does more than just output its own code.
Check out this thread:
Shortest Ruby Quine
Explanation for perl quine
Look at printf
parameters and substitute them by hand,
(39
is single quote, '
, and 10
is newline \n
when interpreted as %c
)
so $a
which start as
$a=%c%s%c;printf($a,39,$a,39,10);%c
becomes (replaced chars marked below with ^
)
$a='%s%c;printf($a,39,$a,39,10);%c
^ (first %c replaced)
$a='$a=%c%s%c;printf($a,39,$a,39,10);%c%c;printf($a,39,$a,39,10);%c
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ (%s replaced)
$a='$a=%c%s%c;printf($a,39,$a,39,10);%c';printf($a,39,$a,39,10);%c
^ (second %c replaced)
and finally
$a='$a=%c%s%c;printf($a,39,$a,39,10);%c';printf($a,39,$a,39,10);\n
(last %c replaced) ^^
Output Ruby class
Sure. You can use the magic constant __FILE__
which contains the path to the file that you use it in:
class MyClass
def initialize
puts File.read(__FILE__)
end
end
This will print the contents of the file containing the definition of MyClass
every time you create a MyClass
object.
What are quines? Any specific purpose to have them?
Quines are useless in a practical sense, but they're a great exercise to help you learn more about a language.
Here's a very concise one in python:
a='a=%r;print a%%a';print a%a
Can a program output a copy of itself
Yes. A programme that can make a copy of itself is called a "quine".
The basic idea of most quines is:
You write code that takes a string literal
s
and prints it, while replacing occurrences (or the occurrence) of a special substring foo ins
by the value ofs
itself.You take the entire source code of the program so far and use it as the definition for
s
. but you exclude the definition ofs
from the string, instead replacing it by foo.
That's the general idea. The rest is string formatting details, really.
Related Topics
Error Installing Debugger: Failed to Build Gem Native Extension with Ruby-1.9.3-P362
Param Is Missing or the Value Is Empty: User Rails 4
Differences Between Proc and Lambda
Failed to Build Gem Native Extension
Rvm Ruby Installation Errors - MAC
Ruby Dsl (Domain Specific Language) Repositories, Examples
How to Wrap the Invocation of a Ruby Method by Including a Module
Rails 3. How to Add a Helper That Activeadmin Will Use
How to Set the Actionmailer Default_Url_Options's :Host Dynamically to the Request's Hostname
Rake Assets:Precompile Attempting to Connect to Database
Scope of Constants in Ruby Modules