Self-replicating Ruby

Dear Ruby fans,

A common Lisp exercise is to write a Lisp program that writes its own
source code to standard output. Reading from a source file is not
permitted.

This leads one to wonder: what is the smallest and / or most elegant
Ruby program that writes its own source code to standard output? As
with Lisp, reading from a file is not permitted. Given the nature of
the language, this problem should should have a lot of interesting
solutions.

On Mon, Oct 22, 2007 at 11:25:02AM +0900, Frank J. Lhota wrote:

solutions.
This is called a Quine, right? I can’t beat Ryan D.', so I’ll just
link to it. :slight_smile:

http://blog.zenspider.com/archives/2006/09/ruby_quine__sli.html

Aaron P. wrote:

the language, this problem should should have a lot of interesting
solutions.

This is called a Quine, right? I can’t beat Ryan D.', so I’ll just
link to it. :slight_smile:

http://blog.zenspider.com/archives/2006/09/ruby_quine__sli.html

I did one in ERb, too:

<%=s=“>%"}esrever.s{#}tcepsni.s{#=s=%<";”;“<%=s=#{s.inspect}#{s.reverse}”%>

On Oct 21, 10:21 pm, “Frank J. Lhota” [email protected]
wrote:

This leads one to wonder: what is the smallest and / or most elegant
Ruby program that writes its own source code to standard output? As
with Lisp, reading from a file is not permitted. Given the nature of
the language, this problem should should have a lot of interesting
solutions.

There’s a whole page of Ruby quines (including 2 of my own) in the
rubygarden wiki: http://rubygarden.org/Ruby/page/show/RubyQuines

On Oct 22, 1:35 pm, Morton G. [email protected] wrote:

with Lisp, reading from a file is not permitted. Given the nature of
the language, this problem should should have a lot of interesting
solutions.

My favorite Ruby quine is

DATA.rewind STDOUT.write(DATA.read) __END__

Very nice. I looked up “DATA” in the pickaxe and found that I had
previously highlighted it - must’ve been a long time ago. Here’s a
rewindless version :slight_smile:

puts DATA.read * 2
END
puts DATA.read * 2
END

On Oct 21, 2007, at 10:25 PM, Frank J. Lhota wrote:

solutions.
My favorite Ruby quine is

DATA.rewind STDOUT.write(DATA.read) __END__

Regards, Morton