17

Just finished reading this blog post: http://www.skorks.com/2010/03/an-interview-question-that-prints-out-its-own-source-code-in-ruby/

In it, the author argues the case for using a quine as an interview question. I'm not sure I agree but thats not what this question is about.

He goes on to construct a quine in Ruby and refactor it to make it shorter. He then challenges the reader to try to make it even shorter.

I played around with it for a while and came up with the following:

s="s=;puts s[0,2]+34.chr+s+34.chr+s[2,36]";puts s[0,2]+34.chr+s+34.chr+s[2,36]

This is the first time I have ever attempted a quine and I can't figure out how to make it any shorter.

What is the shortest Ruby quine you can come up with? Please post an explanation if your implementation requires it.

Jon Limjap
  • 94,284
  • 15
  • 101
  • 152
AaronThomson
  • 763
  • 8
  • 19

2 Answers2

26

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
corvus_192
  • 362
  • 4
  • 15
Michael Kohl
  • 66,324
  • 14
  • 138
  • 158
  • Thanks, thats just what I was looking for. Can you explain the syntax on the last one? – AaronThomson Mar 21 '10 at 04:04
  • It's actually explained on the second page of the thread linked above. Quote: "puts <<2" - print all the text from after this statement until you reach the string "2". ...."*2" - Print that string twice ....",2" - And then print the value 2 The second "puts <<2*2,2" is just text, and the final "2" is the delimiter. – Michael Kohl Mar 21 '10 at 09:07
  • It works on Perl too! http://pastebin.com/0YVrr4wN – Ming-Tang Dec 18 '10 at 05:14
  • 1
    Here's the link Michael Kohl refers to, with the eplanation of the third quine: https://groups.google.com/forum/#!topic/comp.lang.ruby/dhG250t0FxY (see the last 23/10/03 post). – Suzanne Soy Feb 24 '14 at 11:47
  • Nice. All those quines are missing a newline, though. – Eric Duminil Mar 17 '18 at 19:56
  • `_="_=%p;puts _%%_";puts _%_` is great but to understand how it works you'd better replace `_` with normal variable name, say `a` and play with it in `irb` – Wile E. Jan 02 '19 at 06:42
0

Even shorter:

$><<IO.read($0)

15 characters, not including the newline

Jake Summers
  • 49
  • 1
  • 1