-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 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!
on 2009-05-29 17:56
on 2009-05-30 00:18
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
on 2009-05-30 01:01
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
on 2009-05-30 02:19
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.
on 2009-05-30 02:33
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 ;-)
on 2009-05-30 03:26
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_!
on 2009-05-30 03:42
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_!)) ? ;)
on 2009-05-30 04:30
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).
on 2009-05-30 05:22
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_!
on 2009-05-30 05:44
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
on 2009-05-30 06:46
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
on 2009-05-30 07:21
Ok...with help from a friend... If I REALLY (and I do mean REEEEAAALLLYYY) cheat... 8 bytes! - Josh
on 2009-05-30 19:13
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
on 2009-05-30 19:27
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
on 2009-05-31 00:25
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
on 2009-05-31 00:34
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
on 2009-05-31 14:25
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]
on 2009-05-31 17:56
Ok, the shortest solution did not have much success, what about a long one then ;) http://pastie.org/495692 Cheers Robert
on 2009-05-31 18:31
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]
on 2009-05-31 19:11
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 -----
on 2009-05-31 19:26
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]
on 2009-05-31 23:34
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
on 2009-06-01 05:01
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
on 2009-06-01 05:17
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 <
on 2009-06-01 05:17
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
on 2009-06-01 06:00
Is using File considered cheating?
puts File.readlines __FILE__
On Mon, Jun 1, 2009 at 11:17 AM, Aureliano Calvo
on 2009-06-01 06:31
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}`
on 2009-06-01 07:48
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
on 2009-06-01 07:59
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
on 2009-06-01 14:40
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
on 2009-06-01 14:48
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
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.
on 2009-06-01 15:37
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]
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.
on 2009-06-01 16:56
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.
on 2009-06-01 16:56
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]
on 2009-06-01 18:30
Many of these were far more interesting than mine, but it's available here: http://gist.github.com/120242 Nice work everyone! -Dana
on 2009-06-07 20:28
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!
on 2010-12-21 22:23
A jaw dropping quine by Yusuke Endoh: http://mamememo.blogspot.com/2010/09/qlobe.html
on 2010-12-21 23:34
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.
on 2010-12-21 23:39
...although it dosnt seem to work in win7.... ........stoopit windoze! (but still very cool!!)
on 2010-12-24 15:50
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.
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
Log in with Google account | Log in with Yahoo account
No account? Register here.