Ruby, 155 186 195 148 138 110 97 characters
require'open-uri';puts open('http://codegolf.stackexchange.com/posts/28159/body').read[/req.+;/];
I had to make it one line, because otherwise it would output newlines as \n
instead of actual newlines.
+31 characters because I didn't notice some characters were being escaped.
+9 characters to get rid of the annoying backslash.
- Thanks to Nathan Osman for saving 2 chars, and Ventero for saving 55 (!!!) by removing the need for most of the fixes listed above.
The explanation
Let's beautify this a bit first. However, I'm going to have to use a somewhat... interesting notation in this code. I can't use semicolons at all in this post, for reasons explained later, so I will use {SEMI}
in place of semicolons instead.
require 'open-uri'
resp = open('http://codegolf.stackexchange.com/posts/28159/body').read
puts resp.match(/req.+{SEMI}/){SEMI}
Alright, now let's walk through this. The first two lines are fairly self-explanatory -- they fetch the HTML text of this answer.
Now, the last line is the interesting one here. You see that seemingly useless semicolon at the end of the code? It's absolutely required, and here's why.
First, resp.match
extracts the code to be printed. The regexp it uses for this is the trick: /req.+{SEMI}/
. It grabs the start of the code, REQuire'net/http'
, by searching for req
(re
would grab my REputation
). Then, it finds the end of the code by searching for a semicolon! Since +
is greedy by default, it will keep going until it finds the semicolon that signifies the end of the code. See why I can't use semicolons anymore?
After that, I don't have to unescape anything thanks to Ventero's fix of not using \
at all anymore. All I have to do is fix {AMPERSAND}
changing into {AMPERSAND}amp{SEMI}
, which can be achieved simply by removing the amp{SEMI}
part. No need for this anymore because of new URL. After that, the original code has been retrieved! (Note: I can't use the ampersand either, because that gets HTML-encoded which causes a semicolon to be created.)
39Catch-22: How am I supposed to test my submission? – Martin Ender – 2014-05-17T18:11:24.827
9I forsee people posting answers and deleting them, so they can test their code. – Justin – 2014-05-17T18:18:33.327
4@m.buettner answers can be tested on other answers (to other questions) first, then posted, then edited to change the URL :) – aditsu quit because SE is EVIL – 2014-05-17T18:20:24.297
I think the participants should agree on a code of conduct that no two answers will be in the same language with the same character count (i.e. be a gentleman and use an additional character if there's a tie), such that everyone can save characters but not having to scrape for author or ID of the post (so posts can be identified by the headline directly). ;) – Martin Ender – 2014-05-17T18:29:32.237
@m.buettner I'm not sure that would save characters... basically I can allow this technique, but I won't require such conduct – aditsu quit because SE is EVIL – 2014-05-17T18:36:19.237
1I bet the next question will be a program that program itself. – Fabricio – 2014-05-17T19:44:32.020
How does this challenge handle comments designed to interfere with an answer? – Reinstate Monica - ζ-- – 2014-05-17T23:14:46.210
8@hexafraction if comments are able to interfere with an answer, then the answer is not very good... – aditsu quit because SE is EVIL – 2014-05-17T23:41:28.857
Given enough answers one submission might end up on page 2, 3, ... Must the solutions account for this? – Sylwester – 2014-05-18T00:06:22.997
@Sylwester yes I already mentioned that in the question – aditsu quit because SE is EVIL – 2014-05-18T00:13:05.910
17A question stuck in my head: How to write a tweet that links to itself without using any URL shorteners, but by estimating the tweet id your tweet? – Ming-Tang – 2014-05-18T05:35:52.597
3@Sylwester that shouldn't be a problem - if you use the direct link to the answer (instead of the question), the correct page should be open already. – Martin Ender – 2014-05-18T17:55:13.843
Concerning your new rule for JavaScript because it is powerful on this question is just like create a rule "Only language used in production accepted" because GolfScript is not fair for the majority of code-golf questions. Anyway, this is just my opinion... – Michael M. – 2014-05-19T11:42:47.647
1@Michael The rule is just about the circumstances of running the code. It's like saying that a program that prints "/0/1/2/3/.../20" must not depend on being executed in a folder with that exact path. And if only production languages were accepted, somebody WILL use GolfScript in production :) – aditsu quit because SE is EVIL – 2014-05-19T13:03:13.663
is jQuery allowed? (and more broadly, is this commonly allowed/disallowed in this site?) – Martijn – 2014-05-20T09:26:33.190
@Martijn I see no reason to exclude it, as long as it follows the rules – aditsu quit because SE is EVIL – 2014-05-20T09:51:25.070
I proposed the opposite of this a bit ago: http://meta.codegolf.stackexchange.com/questions/1412/proposed-question-sandbox-mark-xii-retired/1528#1528 it never came to fruition.
– Cruncher – 2014-05-20T14:13:13.1731
@m.buettner I love the way your catch-22 comment has exactly 22 upvotes :D Here: http://i.imgur.com/twJzcfa.png
– shortstheory – 2014-05-21T03:06:25.390@SHiNKiROU That does not apply here, as you can post the answer and then edit it to include the url of the answer – Amith KK – 2014-05-21T08:52:00.870