Last active
March 12, 2016 23:10
-
-
Save Gadgetoid/999958ccc28255e6547f to your computer and use it in GitHub Desktop.
Absurdly fudgetastic method of including Python and Ruby in the same file. Will certainly break!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| x=x\ | |
| =begin = 1 | |
| print("Hello World, I'm Python!") | |
| ''' | |
| =end | |
| puts "Hello World, I'm Ruby!" | |
| =begin | |
| ''' | |
| end=1 | |
| x \ | |
| =end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I don't know why I did this, but I felt it should be possible. A quick Google only turned up answers to problems I didn't have, so I chipped away at this slowly- focussing around comment syntax since I figured that would be the likely candidate for abuse.
=beginand=endare used as muti-line comments in Ruby. In most languages they're also the right-hand of a full assignment. Abusing Python's line continuation operator\( backslash ) allowed me to trick Python into thinking I was writing:x=beginThis was a good start, but if you haven't already guessed it yielded me:
NameError: name 'begin' is not definedTo fix this,
=begincan be suffixed with an assignment, resulting in:x=begin=1which exploits a trick to assign multiple variables with the same value. This is constructed like so:
This works either because Ruby considers anything ( as long as it's separated by a space ) after
=beginto be inside the comment, or just ignores anything else on that line altogether.However, now Ruby throws an error:
'<main>': undefined local variable or method 'x' for main:Object (NameError)This is because Ruby's loose rules about parentheses lead
xto be mistaken for a call to a method when it's on its lonesome. Substitute this for:x=x...and it's always interpreted as an assignment to x of a nil value unassigned variable x... Bonkers!
This brings us up to:
Which is an opening Ruby comment that is treated in Python as setting
xandbeginto 1.Commenting out all the Ruby code at this point is easy. Just use a multi-line Python comment
'''. Triple single or triple double quotes, either will do. You'll usually find these as part of DocStrings in Python.Now the whole Ruby body of the script is inside a big Python comment, and any Python immediately before the opening comment will be executed in Python but commented out in Ruby.
But we need to seal the deal, or we'll get something like this in Python:
SyntaxError: EOF while scanning triple-quoted string literalSo we'll just close that triple-quoted string right up:
Or not...
In this instance our
=beginis already commented out in Python. But=endisn't. But we can use the same trick we used above, right?Damn it. Not quite!
Well, this is an easy fudge though?
Yay!
For some reason, this also works. Although I feel it shouldn't: