[QUIZ] Quine (#207)

Daniel Moore wrote:

This week's quiz is to create a quine[1], that is: a program which
receives no input and produces a copy of its own source code as its
only output.

I have mine down to down to one line (29 characters), though I suspect everyone
will agree I'm cheating.

Cool side effect though, adding this line to any other Ruby source file will
turn it into a quine :-p

-Dana

···

--
Dana Merrick - System Administrator
Integrated Computer Solutions, Inc.
54B Middlesex Tpke, Bedford, MA 01730
617.621.0060 x112 - http://www.ics.com

I'm always up for some golf!

12 bytes if *NIX and cheating...HA!

-Josh

There were a great many solutions to the quiz this week. Let's dive in!

Robert Dober, along with others, had the shortest quine at zero bytes.
Robert also had the longest quine. His quine has methods, including a
module, clean formatting, and is even self checking! It checks its
output by reading the file in $0 to make sure it matches.

Matthew Williams had an interesting take: use Ruby2Ruby to handle
translating a class into text.

    require 'rubygems'
    require 'ruby2ruby'

    class Quine < Object
      def initialize
        puts "require 'rubygems'\nrequire
'ruby2ruby'\n\n#{Ruby2Ruby.translate self.class}\nQuine.new\n"
      end
    end
    Quine.new

There were also several classic solutions. Pascal Bourguignon was the
first to submit one and it is a great example:

    q=34;printf a="q=34;printf a=%c%s%c,q,a,q;puts",q,a,q;puts

The `q=34` is a double quote character. The string containing the
program is stored in `a`. Then that string is given to `printf` and
expanded and quoted here: `a=%c%s%c`. The shortest classic solution
was 28 bytes, submitted by Matthew Williams:

    a="a=%p;puts a%%a";puts a%a

All of the classic solutions pass the piping test, suggested by Brian Candler:

    cat quine.rb | ruby

This eliminates tricks with __FILE__ and $0. Speaking of tricks with
__FILE__ and $0 there were also many solutions that used those tricks
effectively. Jorrel submitted a good example of this kind of quine:

    puts File.readlines __FILE__

It's not the shortest but it illustrates the principles nicely.

James Gray demonstrated an interesting use of `DATA`:

    DATA.rewind
    puts DATA.read
    __END__

A solution using $0 comes from Sebastian Hungerecker:

    system"cat",$0

The advantage here is that it doesn't matter if the filename has
spaces or not, but like all of the quines in this category it fails
the piping test because the file is not available for the piped input
stream to read.

And finally, there are the extremely short quines. These usually make
some assumptions about being run on *NIX possibly with some special
setup before hand.

Martin DeMello gets one in at 10 bytes, if you alias `cat` to `c`, 12
bytes if you don't. His solution also requires calling ruby with the
-p option. It also does not pipe successfully but still quite
inventive.

    > echo | ruby -p quine.rb
    $_=`c #$0`

Joshua Ballanco, in a beautiful abuse of the rules, gets down to 8
bytes, and it successfully pipes. There is some setup involved, and it
assumes *NIX, so it is unlikely to work 'out of the box' on most
machines.

    > cat a
    $><<`a`

    > ruby a
    $><<`a`

    > cat a | ruby | ruby
    $><<`a`

    > which a
    /usr/local/bin/a

    > cat `which a`
    #!/usr/bin/env ruby
    puts '$><<`a`'

The environment in which a program reproduces is not easily separable
from the program. When you include all of the implicit assumptions the
requisites for self perpetuation quickly add up. You need version of
Ruby, an operating system with certain features, a file that the code
exists in... In order for a program to truly reproduce it may need to
include a runtime environment, an operating system, maybe even the
hardware to run on. In any case there is not always a dividing line
that can be drawn between an reproducing entity and its environment
without severing reproducibility and that line is usually arbitrary.

I hope you all enjoyed exploring quines this week!

207.tar.gz (3.14 KB)

A jaw dropping quine by Yusuke Endoh:

···

--
Posted via http://www.ruby-forum.com/.

Daniel Moore wrote:
> This week's quiz is to create a quine[1], that is: a program which
> receives no input and produces a copy of its own source code as its
> only output.

I have mine down to down to one line (29 characters), though I
suspect everyone will agree I'm cheating.

I have 30 characters, w/out cheating. It does output a warning
on 1.8.6, but not 1.8.7 or 1.9.1. (And since the warning is on
stderr, it doesn't affect the quine output really, I guess.)

Regards,

Bill

···

From: "Dana Merrick" <dmerrick@ics.com>

I have 19 characters with cheating. 14 if it only needs to run on unixoid-
systems.

···

Am Samstag 30 Mai 2009 00:17:21 schrieb Dana Merrick:

I have mine down to down to one line (29 characters), though I suspect
everyone will agree I'm cheating.

Ok...with help from a friend...

If I REALLY (and I do mean REEEEAAALLLYYY) cheat...

8 bytes!

- Josh

···

On May 29, 2009, at 10:14 PM, Joshua Ballanco wrote:

I'm always up for some golf!

12 bytes if *NIX and cheating...HA!

-Josh

Many of these were far more interesting than mine, but it's available here:

Nice work everyone!

-Dana

···

--
Dana Merrick - System Administrator
Integrated Computer Solutions, Inc.
54B Middlesex Tpke, Bedford, MA 01730
617.621.0060 x112 - http://www.ics.com

Holy shit.

I am in awe.

···

On Dec 21, 2010, at 13:23 , Siep Korteling wrote:

A jaw dropping quine by Yusuke Endoh:
mamememo: The Qlobe

Well, 15 if it needs to work correctly :wink:

···

Am Samstag 30 Mai 2009 02:17:54 schrieb Sebastian Hungerecker:

14 if it only needs to run on unixoid-systems.

i'm impressed :slight_smile: still stuck at 10 chars with extreme cheating

m.

···

On Sat, May 30, 2009 at 10:50 AM, Joshua Ballanco <jballanc@gmail.com> wrote:

Ok...with help from a friend...

If I REALLY (and I do mean REEEEAAALLLYYY) cheat...

8 bytes!

- Josh

On May 29, 2009, at 10:14 PM, Joshua Ballanco wrote:

I'm always up for some golf!

12 bytes if *NIX and cheating...HA!

-Josh

I have 30 characters, w/out cheating. It does output a warning
on 1.8.6, but not 1.8.7 or 1.9.1. (And since the warning is on
stderr, it doesn't affect the quine output really, I guess.)

Here 'tis. It's 29 characters plus a linefeed.

p eval _="print'p eval _=';_"

Regards,

Bill

···

From: "Bill Kelly" <billk@cts.com>

...although it dosnt seem to work in win7....

........stoopit windoze!

(but still very cool!!)

···

On Tue, Dec 21, 2010 at 5:30 PM, Ryan Davis <ryand-ruby@zenspider.com>wrote:

On Dec 21, 2010, at 13:23 , Siep Korteling wrote:

> A jaw dropping quine by Yusuke Endoh:
> mamememo: The Qlobe

Holy shit.

I am in awe.

How about this one ?

Holy ... Merry Christmas !

I think I am in awe too.

···

On 21 December 2010 23:30, Ryan Davis <ryand-ruby@zenspider.com> wrote:

On Dec 21, 2010, at 13:23 , Siep Korteling wrote:

A jaw dropping quine by Yusuke Endoh:
mamememo: The Qlobe

Holy shit.

I am in awe.

Sebastian Hungerecker <sepp2k@googlemail.com> writes:

···

Am Samstag 30 Mai 2009 02:17:54 schrieb Sebastian Hungerecker:

14 if it only needs to run on unixoid-systems.

Well, 15 if it needs to work correctly :wink:

I've got one that is more than 40 characters, but it's _readable_!

--
__Pascal Bourguignon__

Ha, I just made one with 0 chars. Top that!

SCNR,
Sebastian

···

Am Samstag 30 Mai 2009 18:50:11 schrieb Martin DeMello:

i'm impressed :slight_smile: still stuck at 10 chars with extreme cheating

wow... that was cool!!!

···

On Fri, Dec 24, 2010 at 9:50 AM, Benoit Daloze <eregontp@gmail.com> wrote:

On 21 December 2010 23:30, Ryan Davis <ryand-ruby@zenspider.com> wrote:
>
> On Dec 21, 2010, at 13:23 , Siep Korteling wrote:
>
>> A jaw dropping quine by Yusuke Endoh:
>> mamememo: The Qlobe
>
> Holy shit.
>
> I am in awe.
>

How about this one ?
mamememo: Merry Quine-mas 2010

Holy ... Merry Christmas !

I think I am in awe too.

I've got one that is more than 40 characters, but it's _readable_!

Did you mean: ((I've got (one)) (that is more than ((40) characters)), ((but) (it's _readable_!)) ?

:wink:

···

From: "Pascal J. Bourguignon" <pjb@informatimago.com>

I've got one that's 197 characters, but it:

+ is readable and formatted nicely
+ uses gems
+ uses a class
+ shows the inheritence of said class
+ has more than one instance of "end"
+ took me less than 5 minutes to write

:wink:

Now to try for brevity....

Matt

···

--
"... if you do follow your bliss you put yourself on a kind of
track that has been there all the while, waiting for you, and the life
that you ought to be living is the one you are living. When you can
see that, you begin to meet people who are in your field of bliss, and
they open doors to you. I say, follow your bliss and don't be afraid,
and doors will open where you didn't know they were going to be." --
Joseph Campbell

On Sat, 30 May 2009, Pascal J. Bourguignon wrote:

Sebastian Hungerecker <sepp2k@googlemail.com> writes:

Am Samstag 30 Mai 2009 02:17:54 schrieb Sebastian Hungerecker:

14 if it only needs to run on unixoid-systems.

Well, 15 if it needs to work correctly :wink:

I've got one that is more than 40 characters, but it's _readable_!

--
__Pascal Bourguignon__