Ruby: Print source code

2019-09-15 10:42发布

Recently I heard about something called Quine. But my definition of it is a bit unclear.

I believe that you can print your own Ruby file's source code without using __FILE__? Is that possible? I would be very interested in your ideas. I would like to test that out. Dunno, get the source of the code below:

class MyClass
  def initialize
    print "You cannot read me!!"
  end
end

So I can print that whole class?

2条回答
放荡不羁爱自由
2楼-- · 2019-09-15 10:47

Such things should not be called "Quines" but "Gödels" or "Goedels" because (at least so it appears to me) the technique commonly used to achieve that effect is used in Gödel's proof of the First Incompleteness Theorem (cf. Kurt Gödel's Collected Works I, p.175) which came before Quine's discussions.

查看更多
爷的心禁止访问
3楼-- · 2019-09-15 10:55

A Quine is a program that prints out its own source code as its only functionality and yes you can do this without reading the source code file. The real challenge, and fun, is to write as short a Quine as possible.

The above would not be a Quine because it doesn't print its own source code out and it couldn't be because it does more than just output its own code.

Check out this thread:
Shortest Ruby Quine

查看更多
登录 后发表回答