64
\$\begingroup\$

Long-time lurker, first-time poster. So here goes.

In the Wikipedia page for quine, it says that "a quine is considered to be 'cheating' if it looks at its own source code." Your task is to make one of these "cheating quines" that reads its own source code.

This is , so the shortest code in bytes - in each language - wins. This means that a 5-byte Pyth script would not beat a 21-byte Python script - but a 15-byte Python script would.

You must use file I/O to read the source code, so the following JavaScript code, taken from the official Wikipedia page, is invalid:

function a() {
    document.write(a, "a()");
}
a()

It must access the source code of the file on disk.

You are not allowed to specify the file name. You must make it detect the filename itself.

Everyone clear? Go!

\$\endgroup\$
12
  • 1
    \$\begingroup\$ Is a trailing newlines not present in the original file allowed? \$\endgroup\$
    – isaacg
    Oct 29 '15 at 22:04
  • 3
    \$\begingroup\$ @isaacg IMHO That's not a quine, since it is not the source code. \$\endgroup\$
    – user42643
    Oct 29 '15 at 22:05
  • 3
    \$\begingroup\$ You should state a requirement that it determine the actual filename instead of assuming a hard-coded string for the source location. \$\endgroup\$
    – feersum
    Oct 29 '15 at 22:26
  • 3
    \$\begingroup\$ I agree with @feersum though, that requiring a specific file name makes this challenge way to trivial. \$\endgroup\$
    – user42643
    Oct 29 '15 at 22:33
  • 2
    \$\begingroup\$ Can we assume that (for compiled languages) the source code is in the same folder (i.e. we can just add ".cpp" or ".hs" to arg[0] to get the source). \$\endgroup\$
    – HEGX64
    Nov 1 '15 at 9:15

74 Answers 74

1 2
3
0
\$\begingroup\$

A Groovy 103 100 bytes!

Yes I know I'm evil

I stay in line with the other JVM answers (Java, Scala) to make this answer.

class X{static def main(String[]a){new X()}
def X(){print new File(getClass().name+'.groovy').text}}
\$\endgroup\$
0
\$\begingroup\$

Emacs Lisp (93 Bytes)

:; emacs -Q -script $0; exit
(find-file (nth 2 command-line-args))
(message (buffer-string))

The first line calls the program in your normal shell and exits (to prevent the rest also being evaluated). command-line-args will be a list of three strings, "emacs", "-scriptload" and the name of your file. find-file will open that file and make it the current buffer, so that (message (buffer-string)) will write it to stdout as -script redirects messages there.

It should be noted, that -Q may not be necessary if your Emacs doesn't print other messages during startup, so that the first line could be shortened to #!emacs -script, shortening this to 81 bytes, but as it is highly unlikely that init and site-lisp files produce 0 messages at all, this cannot be assumed.

\$\endgroup\$
0
\$\begingroup\$

Matlab, 32 bytes

disp(fileread([mfilename,'.m']))
\$\endgroup\$
2
  • \$\begingroup\$ You are not allowed to assume the file extension is .m. \$\endgroup\$
    – Makonede
    Jul 9 at 20:43
  • \$\begingroup\$ @Makonede MATLAB files have have the ending .m, so it would make sense doing anything other than that. The challenge specifically says we have to read the file name, not the ending, and OP actually confirmed that we can use the file ending. \$\endgroup\$
    – flawr
    Jul 10 at 21:18
0
\$\begingroup\$

APL, 8 bytes (non-competing)

APL does not use source files for single functions, rather a whole workspace (a machine-readable collection of token-representations) is saved and loaded, so I guess this is not allowed:

f     
⎕CR'f'

(⎕CR is Character Representation) as the program reads its current definition in memory (the loaded workspace), which may have been modified from the source in the file.

\$\endgroup\$
0
\$\begingroup\$

HTML and Javascript, 88 Bytes

<head></head><body><svg onload="alert(document.documentElement.innerHTML)"></svg></body>

Had to add <head></head><body></svg></body> because that's what the browser outputs regardless of the original file.

\$\endgroup\$
2
  • \$\begingroup\$ #HTML and Javascript, 109 Bytes# <html><head></head><body onload="alert('<html>'+document.documentElement.innerHTML+'</html>')"></body></html> @Masterzagh, Actually browsers add html tag too. Sorry, Couldn't add a comment as I don't have enough reputation. \$\endgroup\$ Jan 10 '17 at 12:32
  • \$\begingroup\$ I tested it on firefox and chrome and neither added the html (at least on the alert which is what we're interested in). @prathapareddy \$\endgroup\$
    – user64039
    Jan 10 '17 at 16:45
0
\$\begingroup\$

Lua, 32 29 bytes

3 bytes saved thanks to Jonathan Frech

print(io.open(arg[0]):read())

Try it online!

How?

Lua uses the arg table to store it's arguments, the script name is always stored at index 0 of this table despite the fact there are no further arguments. After using this table to get the name of the file, printing it was as easy as opening the file and reading its content.

\$\endgroup\$
2
  • \$\begingroup\$ I think you can toss your variable and save three bytes. \$\endgroup\$ Sep 24 '18 at 20:40
  • \$\begingroup\$ It is interesting, how your script's file name is stored at 'index' zero, even though LUA is 1-indexed ... And it goes on: arg[-1] contains the language name ... \$\endgroup\$ Sep 24 '18 at 21:49
0
\$\begingroup\$

Perl 6 / POSIX shell, 16 bytes

run 'cat',$?FILE

Try it online!

\$\endgroup\$
0
\$\begingroup\$

CopyPasta Language, 32 bytes

But I think this is interesting enough to compete here.

CopyFile
TheFuckingCode
Pasta!

Copy the source code, display the clipboard, and stop the program.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Worthy of note: This language did not have an interpreter when you posted this, so it would have been invalid. As of rn, I have implemented an interpreter for the language, since the linked interpreter on the esolang is a dead link \$\endgroup\$ Oct 3 '19 at 3:06
0
\$\begingroup\$

stacked, 11 bytes

program put

Try it here!

Outputs the program stored in the program variable. Simple!

\$\endgroup\$
0
0
\$\begingroup\$

Jelly u, 26 bytes

“open(sys.argv[2])”ŒV

Try it online!

Not encoded in Jelly's SBCS, because 1. by meta consensus on flags Jelly u is a different language from Jelly (so it's not like I'm handicapping myself or anything), 2. I'm having some difficulties deciding on what form of output would be valid for an SBCS Jelly cheating quine, and 3. I'm having some difficulties actually executing on the methods I've considered (note that that's not even shorter than this).

“open(sys.argv[2])”ŒV    Evaluate "open(sys.argv[2])" as Python, within Jelly's interpreter.py.

      sys.argv           Get the arguments with which this was invoked on the command line.
                         (jelly fu [whatever the file's called])
              [2]        Take the third argument,
 open(           )       and open the file at that location.

                   ŒV    Iterate through the file object, obtaining a Jelly string of its contents.
\$\endgroup\$
4
  • \$\begingroup\$ This can be encoded in the SBCS and be perfectly valid at the same time. \$\endgroup\$
    – Makonede
    Jul 8 at 18:04
  • \$\begingroup\$ @Makonede It still presents the difficulty of taking SBCS-encoded file contents and outputting either the corresponding UTF-8 or the same bytes. \$\endgroup\$ Jul 8 at 18:13
  • \$\begingroup\$ How about this: The output should look identical to the program. So the corresponding UTF-8. \$\endgroup\$
    – Makonede
    Jul 8 at 18:18
  • \$\begingroup\$ ...I was about to say "well guess what I already tried", but it turns out the apparent failure is an issue with TIO's bash rather than the solution itself. Glad I tried that again \$\endgroup\$ Jul 8 at 18:37
0
\$\begingroup\$

05AB1E (legacy), 43 39 30 27 bytes

’ƒÛ(__£Ø__('Ë‚').Òœv[1])’.E

Try it online! I would've used the rewrite, if only I knew Elixir… Output doesn't contain the program, however it is the program in a singleton iterable object (a file). This is allowed.

’ƒÛ(__£Ø__('Ë‚').Òœv[1])’.E  # full program
’ƒÛ(__£Ø__('Ë‚').Òœv[1])’    # push "open(__import__('sys').argv[1])"...
                         .E  # evaluated as Python code
                             # implicit output

============================================================================

open(__import__('sys').argv[1])  # full Python program
open(                         )  # return file at path...
                           [1]   # second...
     __import__('sys').argv      # command-line argument

Note: This program assumes no command-line flags or extra arguments are passed. If this assumption is not allowed, it can be fixed at a cost of 1 byte by adding a - before the 1.

\$\endgroup\$
0
\$\begingroup\$

Julia 1.0, 29 26 bytes

print(readline(@__FILE__))

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Processing, 48 bytes

print(loadStrings(getClass().getName()+".pde"));
\$\endgroup\$
3
  • \$\begingroup\$ I don't know Processing, but this looks like it wouldn't work if the file name didn't end in .pde. \$\endgroup\$
    – Makonede
    Jul 8 at 23:11
  • \$\begingroup\$ The java answer sais , getClass().getName()+".java" Also I don't know if there is a way of getting the extension, you can get a list of filenames in a folder but it's not bulletproof either \$\endgroup\$ Jul 9 at 16:01
  • \$\begingroup\$ Then the Java answer is invalid too. If there's no guaranteed way, too bad. \$\endgroup\$
    – Makonede
    Jul 9 at 20:40
0
\$\begingroup\$

Nim, 43 bytes

import os
echo readFile(paramStr(0)&".nim")
\$\endgroup\$
1
  • \$\begingroup\$ See my comment on the above answer. You are not allowed to assume the filename ends in .nim. \$\endgroup\$
    – Makonede
    Jul 9 at 20:41
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.