this page is dedicated to the question: what would be the shortest Ruby program to output itself? and to quines written in ruby in general. the asterisk (*) means that the authors of this page assume the shortest ruby quine to be of nonzero length.
:quine: /kwi:n/ /n./ [from the name of the logician Willard van Orman Quine, via Douglas Hofstadter] A program that generates a copy of its own source text as its complete output. Devising the shortest possible quine in some given programming language is a common hackish amusement. [1]
At the time the shortest of known RubyQuines is 28 characters long:
puts <<2*2,2 puts <<2*2,2 2
by sabbyxtabby at yahoo dot com
a Ruby version of Robin Houston's Perl quine. 28 characters (including the newline at the end)
_="_=%p;puts _%%_";puts _%_
by RyanDavis (2006-09-04). Adapted from python quine (and shorter too!) Documented at http://blog.zenspider.com/archives/2006/09/ruby_quine__sli.html
eval s=%q(puts"eval s=%q(#{s})")
length: 33 (including the newline at the end)eval$s=%q(puts"eval$s=%q(#$s)")by HenoN ["meinrad","recheis@gmx","at"].join(".") (October 18, 2003)
_=%q(puts"_=%q(#{_});eval _;");eval _;
(Translated from Perl.) size 39 (including the newline)
"Ben Tilly" <ben_tilly@hotmail.com>
can be shortened by one by using a global variable. --HenoN
s="s=%c%s%c; printf s,34,s,34,10%c"; printf s,34,s,34,10
size 56. (a ripoff of a famous quine in C, working only on ASCII machines)
a=<<'EOF' print "a=<<'EOF'" print a print "EOF\n" print a EOF print "a=<<'EOF'" print a print "EOF\n" print a
size 109
by Hal Fulton.
all quines from Hal: HTTP://hypermetrics.com/ruby-quine.html
s=%(s=%(x);s['x']=s;puts s);s['x']=s;puts s
size 44 (including final newline).
Here's one that is modelled after the quine presented by Douglas R. Hofstadter on page 498 of "Godel, Escher, Bach":
def quine(s)
print s, 'quine <<d
', s, 'd
'
end
quine <<d
def quine(s)
print s, 'quine <<d
', s, 'd
'
end
d
size 116 including final newline. Size can be reduced by 3 by removing final newline. Size can be further reduced by 20 if the method "quine" is renamed to "q", but minimum size wasn't the main goal.
Both of these are by [Karl von Laudermann] (karlvonl(a)rcn.com).
DATA.rewind puts DATA.read __END__
A very transparent one, ported from the OCaml example of the Wikipedia "quines" page, [2]:
proc {|x| printf "%s[%s]", x, x.inspect}["proc {|x| printf \"%s[%s]\", x, x.inspect}"]
Csaba Henk -- "ekho@rubbishrenyi.hu".sub(/rubbish/,"")
A small one: size 60 (including final newline)
x="x=%s;puts x%%[34.chr+x+34.chr]";puts x%[34.chr+x+34.chr]
...and a big one: See HTTP://www.plenz.com/tmp/code/ruby/quine.rb for a quine, really easy to understand and to write, even for really big programs...
But the smallest Ruby quine must certainly be the following: 'touch empty', for 'ruby empty' outputs absolutely nothing.
size 0
Isaac S -- "ln.avu.ecneics@arajisbi".reverse
cheats
loading files or using special interpreter options is regarded as cheating, however this one proposed by gabriele renzi is very nice:
print IO.read$0length: 15
William Taysom: In much the same spirit, there's the program that prints itself when given itself as input:
puts gets
(lambda (x) (list x (list (quote quote) x))) (quote (lambda (x) (list x (list (quote quote) x)))))
Is:
def x(s); puts %Q{#{s} x(%q{#{s}})}; end; x(%q{def x(s); puts %Q{#{s} x(%q{#{s}})}; end;})
Removing the unneeded whitespace clocks this one in at 84 characters.
-Topher Cyll http://cyll.org
25.times{putc rand(126)}
is (albeit very rarely) the shortest ruby quine at 25 chracters (including newline). But it is _very_ unlikely that there exists an integer seed to rand() that will produce the exact combination out of the 126^25 possible ones. If it were possible to generate arbitrary code like this by specifying some seed, then we'd have a compression algorithm from said arbitrary code to just that single integer. http://en.wikipedia.org/wiki/Pigeonhole_principle
-arsheive