Forum: Ruby Quine (#207)

Posted by Daniel X Moore (yahivin)
on 2009-05-29 17:56
(Received via mailing list)
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

The three rules of Ruby Quiz:

1.  Please do not post any solutions or spoiler discussion for this
quiz until 48 hours have elapsed from the time this message was
sent.

2.  Support Ruby Quiz by submitting ideas and responses
as often as you can!
Visit: http://rubyquiz.strd6.com/suggestions

3.  Enjoy!

Suggestion:  A [QUIZ] in the subject of emails about the problem
helps everyone on Ruby Talk follow the discussion.  Please reply to
the original quiz message, if you can.

RSS Feed: http://rubyquiz.strd6.com/quizzes.rss

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

## Quine (#207)

Ahn nyeong Rubyists,

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.


[1]: http://en.wikipedia.org/wiki/Quine_(computing)

Have Fun!
Posted by Dana Merrick (Guest)
on 2009-05-30 00:18
Attachment: signature.asc (187 Bytes)
(Received via mailing list)
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
Posted by Bill Kelly (Guest)
on 2009-05-30 01:01
(Received via mailing list)
From: "Dana Merrick" <dmerrick@ics.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
Posted by Sebastian Hungerecker (Guest)
on 2009-05-30 02:19
(Received via mailing list)
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.

I have 19 characters with cheating. 14 if it only needs to run on 
unixoid-
systems.
Posted by Sebastian Hungerecker (Guest)
on 2009-05-30 02:33
(Received via mailing list)
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 ;-)
Posted by Pascal J. Bourguignon (Guest)
on 2009-05-30 03:26
(Received via mailing list)
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 ;-)

I've got one that is more than 40 characters, but it's _readable_!
Posted by Bill Kelly (Guest)
on 2009-05-30 03:42
(Received via mailing list)
From: "Pascal J. Bourguignon" <pjb@informatimago.com>
> 
> 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_!)) ?


;)
Posted by Pascal J. Bourguignon (Guest)
on 2009-05-30 04:30
(Received via mailing list)
Bill Kelly <billk@cts.com> writes:

> From: "Pascal J. Bourguignon" <pjb@informatimago.com>
>> 
>> 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_!)) ?
>
>
> ;)

Well I could have done something like that, yes, but I restrained myself 
this time :-)


Too bad we've got only oneliners, a subsidiary questions would have
been to produce a reversed quine, a quine that outputs the source
lines in the reverse order.  (In C it's funny).
Posted by Joel VanderWerf (Guest)
on 2009-05-30 05:22
(Received via mailing list)
Bill Kelly wrote:
> 
> From: "Pascal J. Bourguignon" <pjb@informatimago.com>
>>
>> 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_!)) ?
> 
> 
> ;)

There is only one way to respond to that mess of redundant )

(
   (I've got
     (one
   (that is more than
     (
       (40
       characters,
   (
     (but
     (it's _readable_!
Posted by Matthew K. Williams (Guest)
on 2009-05-30 05:44
(Received via mailing list)
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

;-)

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
Posted by Matthew K. Williams (Guest)
on 2009-05-30 06:46
(Received via mailing list)
Ok, I'm at 17 if I ***cheat*** & assume *NIX.
and 28 if I don't cheat.

Still kinda fun, though....  the one at 28 bytes is not anything I'd use
in real life, however.  Definitely not maintainable.

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
Posted by Joshua Ballanco (jballanc)
on 2009-05-30 07:15
(Received via mailing list)
I'm always up for some golf!

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

-Josh
Posted by Joshua Ballanco (jballanc)
on 2009-05-30 07:21
(Received via mailing list)
Ok...with help from a friend...

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

8 bytes!

- Josh
Posted by Martin DeMello (Guest)
on 2009-05-30 18:56
(Received via mailing list)
i'm impressed :) still stuck at 10 chars with extreme  cheating

m.
Posted by Sebastian Hungerecker (Guest)
on 2009-05-30 19:13
(Received via mailing list)
Am Samstag 30 Mai 2009 18:50:11 schrieb Martin DeMello:
> i'm impressed :) still stuck at 10 chars with extreme  cheating

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

SCNR,
Sebastian
Posted by Bill Kelly (Guest)
on 2009-05-30 19:27
(Received via mailing list)
From: "Sebastian Hungerecker" <sepp2k@googlemail.com>
> 
> Ha, I just made one with 0 chars. Top that!

Hehe.

We did quines on ruby-talk back in 2002, and that is
indeed where we ended up.

(Although, personally, I tend to find nonzero length
quines more interesting)  ;D


Regards,

Bill
Posted by Robert Dober (Guest)
on 2009-05-31 00:18
(Received via mailing list)
0 bytes, what else?
Posted by Rick Denatale (rdenatale)
on 2009-05-31 00:25
(Received via mailing list)
On Sat, May 30, 2009 at 6:18 PM, Robert Dober <robert.dober@gmail.com> 
wrote:
> 0 bytes, what else?

And how do you execute it and prove it works, mon ami? <G>


--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
Posted by Martin DeMello (Guest)
on 2009-05-31 00:34
(Received via mailing list)
On Sun, May 31, 2009 at 3:51 AM, Rick DeNatale <rick.denatale@gmail.com> 
wrote:
> On Sat, May 30, 2009 at 6:18 PM, Robert Dober <robert.dober@gmail.com> wrote:
>> 0 bytes, what else?
>
> And how do you execute it and prove it works, mon ami? <G>

I'd show you but the spoiler-free period hasn't expired yet :)

martin
Posted by Robert Dober (Guest)
on 2009-05-31 14:25
(Received via mailing list)
On Sun, May 31, 2009 at 12:21 AM, Rick DeNatale 
<rick.denatale@gmail.com> wrote:
> On Sat, May 30, 2009 at 6:18 PM, Robert Dober <robert.dober@gmail.com> wrote:
>> 0 bytes, what else?
>
> And how do you execute it and prove it works, mon ami? <G>
I have foreseen everything ;)

507/7 > touch prog && ruby prog > xxx && diff prog xxx
robert@roma:~/log/ruby/quiz 14:24:48
508/8 > echo $?
0





--
Toutes les grandes personnes ont d’abord été des enfants, mais peu
d’entre elles s’en souviennent.

All adults have been children first, but not many remember.

[Antoine de Saint-Exupéry]
Posted by Robert Dober (Guest)
on 2009-05-31 17:56
(Received via mailing list)
Ok, the shortest solution did not have much success, what about a long
one then ;)

http://pastie.org/495692

Cheers
Robert
Posted by Robert Dober (Guest)
on 2009-05-31 18:31
(Received via mailing list)
On Sun, May 31, 2009 at 12:34 AM, Martin DeMello
<martindemello@gmail.com> wrote:
>
Bummer I forgot that there is only one program of size 0, apologies ;)


--
Toutes les grandes personnes ont d’abord été des enfants, mais peu
d’entre elles s’en souviennent.

All adults have been children first, but not many remember.

[Antoine de Saint-Exupéry]
Posted by Pascal J. Bourguignon (Guest)
on 2009-05-31 19:11
(Received via mailing list)
Robert Dober <robert.dober@gmail.com> writes:

> Ok, the shortest solution did not have much success, what about a long
> one then ;)
>
> http://pastie.org/495692


$ echo ----- ; cat quine.rb ; echo ----- ; ruby quine.rb ; echo -----
Posted by Robert Dober (Guest)
on 2009-05-31 19:26
(Received via mailing list)
On Sun, May 31, 2009 at 7:10 PM, Pascal J. Bourguignon
<pjb@informatimago.com> wrote:
> q=34;printf a="q=34;printf a=%c%s%c,q,a,q;puts",q,a,q;puts
> -----
> q=34;printf a="q=34;printf a=%c%s%c,q,a,q;puts",q,a,q;puts
> -----
>
Seems you managed without parens ;)

> --
> __Pascal Bourguignon__
>
>



--
Toutes les grandes personnes ont d’abord été des enfants, mais peu
d’entre elles s’en souviennent.

All adults have been children first, but not many remember.

[Antoine de Saint-Exupéry]
Posted by Bill Kelly (Guest)
on 2009-05-31 23:34
(Received via mailing list)
From: "Bill Kelly" <billk@cts.com>
> 
> 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
Posted by Heesob Park (phasis)
on 2009-06-01 05:01
(Received via mailing list)
2009/6/1 Pascal J. Bourguignon <pjb@informatimago.com>:
> q=34;printf a="q=34;printf a=%c%s%c,q,a,q;puts",q,a,q;puts
> -----
> q=34;printf a="q=34;printf a=%c%s%c,q,a,q;puts",q,a,q;puts
> -----
>
Here is a variant version of the above:

puts (_="puts (_=%c%s%c)%%[34,_,34]")%[34,_,34]


Regards,
Park Heesob
Posted by Aureliano Calvo (Guest)
on 2009-06-01 05:17
(Received via mailing list)
I did something like that, but with parenthesis.

s="s=%c%s%c;printf(s,34,s,34)";printf(s,34,s,34)

It is based on the C quine that uses printf on the wikipedia page.


On Sun, May 31, 2009 at 2:10 PM, Pascal J. Bourguignon <
Posted by Aureliano Calvo (Guest)
on 2009-06-01 05:17
(Received via mailing list)
And I did one with a carriage return:

s="s=%c%s%c;printf(s,34,s,34,13,10)%c%c";printf(s,34,s,34,13,10)


A little longer but looks nicer


On Mon, Jun 1, 2009 at 12:16 AM, Aureliano Calvo
Posted by Jorrel (Guest)
on 2009-06-01 06:00
(Received via mailing list)
Is using File considered cheating?

    puts File.readlines __FILE__


On Mon, Jun 1, 2009 at 11:17 AM, Aureliano Calvo
Posted by Matthew Williams (Guest)
on 2009-06-01 06:31
(Received via mailing list)
Version 1:  The "pretty" one with 'end's

require 'rubygems'
require 'ruby2ruby'

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


Version two, much less maintainable:

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

Version 3, cheating (unix only):

puts `cat #{$0}`
Posted by Martin DeMello (Guest)
on 2009-06-01 07:48
(Received via mailing list)
On Mon, Jun 1, 2009 at 10:00 AM, Matthew Williams <matt@harpstar.com> 
wrote:
>
> Version 3, cheating (unix only):
>
> puts `cat #{$0}`

here's my extremely cheating 10-byte version:

# sudo ln -s /bin/cat /usr/local/bin/c
# echo | ruby -p q.rb
$_=`c #$0`

martin
Posted by Joshua Ballanco (jballanc)
on 2009-06-01 07:59
(Received via mailing list)
On May 31, 2009, at 10:45 PM, Martin DeMello wrote:

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

That was essentially my original cheat, then my friend pointed out
this simple modification to get to 8 bytes:

 > cat a
$><<`a`

 > ruby a
$><<`a`

 > which a
/usr/local/bin/a

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


- Josh
Posted by Sebastian Hungerecker (Guest)
on 2009-06-01 14:40
(Received via mailing list)
Am Montag 01 Juni 2009 06:30:16 schrieb Matthew Williams:
> puts `cat #{$0}`

The problem with that is that it won't work when the file name contains 
spaces
or shell meta-characters. This will however:
system"cat",$0
Posted by James Gray (bbazzarrakk)
on 2009-06-01 14:48
(Received via mailing list)
On May 31, 2009, at 10:59 PM, Jorrel wrote:

> Is using File considered cheating?
>
>    puts File.readlines __FILE__

It usually is, yes.  Your version can be shortened though:

   puts File.read __FILE__

You can also do it more creatively, if a bit longer:

   DATA.rewind
   puts DATA.read
   __END__

James Edward Gray II
Posted by Brian Candler (candlerb)
on 2009-06-01 15:18
James Gray wrote:
> On May 31, 2009, at 10:59 PM, Jorrel wrote:
> 
>> Is using File considered cheating?
>>
>>    puts File.readlines __FILE__
> 
> It usually is, yes.

I'd suggest you pipe the source code into the ruby interpreter:

    cat quine.rb | ruby

    cat quine.rb | ruby | diff -u quine.rb -

That eliminates tricks with __FILE__ and $0.
Posted by Robert Dober (Guest)
on 2009-06-01 15:37
(Received via mailing list)
On Mon, Jun 1, 2009 at 3:20 PM, Brian Candler <b.candler@pobox.com> 
wrote:
>
>    cat quine.rb | ruby
>
>    cat quine.rb | ruby | diff -u quine.rb -
>
> That eliminates tricks with __FILE__ and $0.
>
Good thinking, but the "optimal" solution still passes ;)

 > xxx | ruby | diff -u xxx -

N.B. this ">" ain't a quote ;)
Robert
--
Toutes les grandes personnes ont d’abord été des enfants, mais peu
d’entre elles s’en souviennent.

All adults have been children first, but not many remember.

[Antoine de Saint-Exupéry]
Posted by Brian Candler (candlerb)
on 2009-06-01 15:54
Robert Dober wrote:
> On Mon, Jun 1, 2009 at 3:20 PM, Brian Candler <b.candler@pobox.com> 
> wrote:
>>
>> � �cat quine.rb | ruby
>>
>> � �cat quine.rb | ruby | diff -u quine.rb -
>>
>> That eliminates tricks with __FILE__ and $0.
>>
> Good thinking, but the "optimal" solution still passes ;)

Sure - the trivial quine is valid. I can also run it on my network which 
has zero nodes.
Posted by Pascal J. Bourguignon (Guest)
on 2009-06-01 16:56
(Received via mailing list)
Brian Candler <b.candler@pobox.com> writes:

>
>     cat quine.rb | ruby
>
>     cat quine.rb | ruby | diff -u quine.rb -
>
> That eliminates tricks with __FILE__ and $0.

A better solution would be to compile the ruby program. Then $0 would
be a binary file and that trick would fail.
Posted by Robert Dober (Guest)
on 2009-06-01 16:56
(Received via mailing list)
On Mon, Jun 1, 2009 at 3:54 PM, Brian Candler <b.candler@pobox.com> 
wrote:
>> Good thinking, but the "optimal" solution still passes ;)
>
> Sure - the trivial quine is valid. I can also run it on my network which
> has zero nodes.
True but you cannot test it on your network with zero nodes, but I
have a network with zero nodes too, maybe we can come up with
something like Map Reduce ( or was that Collect Inject :-O)
> --
> Posted via http://www.ruby-forum.com/.
>
>



--
Toutes les grandes personnes ont d’abord été des enfants, mais peu
d’entre elles s’en souviennent.

All adults have been children first, but not many remember.

[Antoine de Saint-Exupéry]
Posted by Dana Merrick (Guest)
on 2009-06-01 18:30
Attachment: signature.asc (187 Bytes)
(Received via mailing list)
Many of these were far more interesting than mine, but it's available 
here:

http://gist.github.com/120242


Nice work everyone!

-Dana
Posted by Daniel X Moore (yahivin)
on 2009-06-07 20:28
Attachment: 207.tar.gz (3,14 KB)
(Received via mailing list)
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!
Posted by Siep Korteling (steenslag)
on 2010-12-21 22:23
A jaw dropping quine by Yusuke Endoh:
http://mamememo.blogspot.com/2010/09/qlobe.html
Posted by Ryan Davis (Guest)
on 2010-12-21 23:34
(Received via mailing list)
On Dec 21, 2010, at 13:23 , Siep Korteling wrote:

> A jaw dropping quine by Yusuke Endoh:
> http://mamememo.blogspot.com/2010/09/qlobe.html

Holy shit.

I am in awe.
Posted by .serialhex .. (serialhex)
on 2010-12-21 23:39
(Received via mailing list)
...although it dosnt seem to work in win7....

........stoopit windoze!

(but still very cool!!)
Posted by Benoit Daloze (Guest)
on 2010-12-24 15:50
(Received via mailing list)
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:
>> http://mamememo.blogspot.com/2010/09/qlobe.html
>
> Holy shit.
>
> I am in awe.
>

How about this one ?
http://mamememo.blogspot.com/2010/12/merry-quine-mas-2010.html

Holy ... Merry Christmas !

I think I am in awe too.
Posted by .serialhex .. (serialhex)
on 2010-12-24 16:00
(Received via mailing list)
wow... that was cool!!!
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.