# Self-replicating Ruby

**URL:** <https://www.ruby-forum.com/t/self-replicating-ruby/117884>\
**Category:** Ruby\
**Created:** [October 22, 2007, 4:25am UTC](https://www.ruby-forum.com/t/self-replicating-ruby/117884 "2007-10-22T04:25:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frank\_JSLhota](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/8e7dd6/32.png) [@Frank\_JSLhota](https://www.ruby-forum.com/u/Frank_JSLhota)\
**Post date:** [October 22, 2007, 4:25am UTC](https://www.ruby-forum.com/t/self-replicating-ruby/117884/1 "2007-10-22T04:25:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Frank\_JSLhota](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/8e7dd6/32.png) [@Frank\_JSLhota](https://www.ruby-forum.com/u/Frank_JSLhota)\
**Post date:** [October 22, 2007, 4:35am UTC](https://www.ruby-forum.com/t/self-replicating-ruby/117884/2 "2007-10-22T04:35:11Z")

</div>

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. 🙂

[http://blog.zenspider.com/archives/2006/09/ruby\_quine\_\_sli.html](http://blog.zenspider.com/archives/2006/09/ruby_quine__sli.html)

---

<div class="post-metadata">

**Author:** ![Frank\_JSLhota](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/8e7dd6/32.png) [@Frank\_JSLhota](https://www.ruby-forum.com/u/Frank_JSLhota)\
**Post date:** [October 22, 2007, 12:37pm UTC](https://www.ruby-forum.com/t/self-replicating-ruby/117884/3 "2007-10-22T12:37:03Z")

</div>

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. 🙂
> 
> [http://blog.zenspider.com/archives/2006/09/ruby\_quine\_\_sli.html](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}”%\>

---

<div class="post-metadata">

**Author:** ![Frank\_JSLhota](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/8e7dd6/32.png) [@Frank\_JSLhota](https://www.ruby-forum.com/u/Frank_JSLhota)\
**Post date:** [October 22, 2007, 8:05pm UTC](https://www.ruby-forum.com/t/self-replicating-ruby/117884/4 "2007-10-22T20:05:31Z")

</div>

On Oct 21, 10:21 pm, “Frank J. Lhota” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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](http://rubygarden.org/Ruby/page/show/RubyQuines)

---

<div class="post-metadata">

**Author:** ![Frank\_JSLhota](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/8e7dd6/32.png) [@Frank\_JSLhota](https://www.ruby-forum.com/u/Frank_JSLhota)\
**Post date:** [October 22, 2007, 9:50pm UTC](https://www.ruby-forum.com/t/self-replicating-ruby/117884/5 "2007-10-22T21:50:58Z")

</div>

On Oct 22, 1:35 pm, Morton G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 🙂

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

---

<div class="post-metadata">

**Author:** ![Frank\_JSLhota](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/8e7dd6/32.png) [@Frank\_JSLhota](https://www.ruby-forum.com/u/Frank_JSLhota)\
**Post date:** [October 22, 2007, 7:37pm UTC](https://www.ruby-forum.com/t/self-replicating-ruby/117884/6 "2007-10-22T19:37:21Z")

</div>

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
